views:

58

answers:

2

I've asked one question about this a month ago, it's here: http://stackoverflow.com/questions/3362399/post-method-to-communicate-directly-with-a-server.

And I still didn't get the reason why sometimes I get 404 error and sometimes everything works fine, I mean I've tried those codes with several different wordpress blogs. Using firefox or IE, you can post the comment without any problem whatever wordpress blog it is, but using python and "post" method directly communicating with a server I got 404 with several blogs. And I've tried to spoof the headers, adding cookies in the code, but the result remains the same. It's bugging me for quite a while... Anybody knows the reason? Or what code should I add to make the program works just like a browser such as firefox or IE etc ? Hopefully you guys would help me out!

A: 

You should use somthing like mechanize.

leoluk
Would you be more specific please? Why some work and some don't, I really want to know the reason behind all this
Shane
mechanize is a browser wrapper that does all the post stuff for you, just read the docs
leoluk
@leoluk: I finally found what my problem is, with the help of Live Header. I noticed that after comment submitting, it will be redirected to the page such as "http://localhost/new-post#comment-29", and urllib2.urlopen will raise a 404 when trying to open this page. That's why the comments show up while I get 404. Mechanize indeed make things much easier now. Thanks man!
Shane
A: 

The blog may have some spam protection against this kind of posting. ( Using programmatic post without accessing/reading the page can be easily detected using javascript protection ). But if it's the case, I'm surprised you receive a 404...

Anyway, if you wanna simulate a real browser, the best way is to use a real browser remote controlled by python.

Check out WebDriver (http://seleniumhq.org/docs/09_webdriver.html) It has a python implementation and can run HtmlUnit, chrome, IE and Firefox browsers.

Spark
The weird thing is, if it's spam protection, how come the comments still show up while you get a 404? I just wonder why and couldn't figure it out...
Shane
You didnt mention that. Then it can't be spam protection. Does the problem appears randomly or is it alway on the same(s) blogs ?
Spark
Always on the same blog. And the funny thing is if you try to make the same comment on the blog the second time, you get a 500, the only difference is that your comment won't show up this time. But if I use a browser, as long as I don't post too quickly, I can always post the exact same comment.
Shane