views:

75

answers:

2

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':'[email protected]',  '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)
A: 

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 ?

makapuf
You can leave a website which will be the link your name directs to when the comments show up, it can be blank. "Author" and "email" must be filled. I'm on windows XP. I've replaced localhost with 127.0.0.1, and the problem remains the same. The problem is the script does post comments every time it runs, but I don't know why it raises error 404.
Shane
A: 

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

monstru0
Thanks for your recommendation, some online wordpress blogs do work with above code, still don't know why it doesn't work in my local site.
Shane
@monstru0: Thanks a lot. Mechanize does make things much easier, and I also found what the problem is finally. Check it out: http://stackoverflow.com/questions/3653086/whats-the-difference-between-browser-posting-and-program-posting
Shane