“post” method to communicate directly with a server

别来无恙 提交于 2019-12-10 12:19:31

问题


Just started with python not long ago, and I'm learning to use "post" method to communicate directly with a server. A fun script I'm working on right now is to post comments on wordpress. The script does post comments on my local site, but I don't know why it raises HTTP Error 404 which means page not found. Here's my code, please help me find what's wrong:

import urllib2
import urllib

url='http://localhost/wp-comments-post.php'
user_agent='Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'  
values={'author':'Urllib Test', 'email':'test@test.com',  'url':'', 'comment':'This is a test comment from python', 'submit':'Post Comment', 'comment_post_ID': '1', 'comment_parent':'0'}  
headers={'User-Agent': user_agent}

data=urllib.urlencode(values)  
req=urllib2.Request(url, data, headers)

urllib2.urlopen(req)

回答1:


I recommend you to use Mechanize. It will simplify your life.




回答2:


why is there a 'url' in your values ? did you try without it ?

then, try replacing localhost with 127.0.0.1 (if localhost is not in your hosts file). Are you on windows or linux ?



来源:https://stackoverflow.com/questions/3362399/post-method-to-communicate-directly-with-a-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!