1

I am using the requests library to try and post on a phpBB forum.

My code looks something like the following:

self.r = requests.session()
d = self.r.get('http://examplephpbbforum.com/community/posting.php?mode=reply&f=' + forumid + '&t=' + topicid)

sid = stringBetween('style.php?sid=', '&id=2', d.content)

lastclick = stringBetween('lastclick" value="', '" />', d.content)
creation_time = stringBetween('creation_time" value="', '" />', d.content)
form_token = stringBetween('form_token" value="', '" />', d.content)
topic_cur_post_id = stringBetween('topic_cur_post_id" value="', '" />', d.content)
payload = {'addbbcode20':'100', 'message':message, 'topic_cur_post_id':topic_cur_post_id, 'lastclick':lastclick, 'post':'Submit', 'attach_sig':'on', 'creation_time':creation_time, 'form_token':form_token}
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1', 'Referer':'http://examplephpbbforum.com/community/posting.php?mode=reply&f=' + forumid + '&t=' + topicid, 'Content-Type':'application/x-www-form-urlencoded', 'Connection':'Keep-alive', 'Host':'examplephpbbforum.com', 'Accept-Language':'en-us,en;q=0.5', 'Accept-Encoding':'gzip, deflate', 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'}

d = self.r.post('http://examplephpbbforum.com/community/posting.php?mode=reply&f=' + forumid + '&sid=' + sid + '&t=' + topicid, data=payload, headers=headers, allow_redirects=True)

Then I check whether the topic was posted successfully by searching for a string in the response.

The flow of the traffic is that after the first post, there is a 302 redirect which the request then follows to the page it successfully posted to.

Sometimes it works (maybe 1 in 50 times) but generally it doesn't work. The response is just the page from the first get request. (The post a reply page)

It should redirect to the posted message pretty much every time, but it doesn't for some reason.

If I sniff the traffic with wireshark it seems the response is generally sent with a URL to redirect to, but it seems something goes wrong there.

Anyone know what might be causing this to fail in the first place and how to fix it?

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Dangeur
  • 11
  • 1
  • 4

1 Answers1

3

I think your problem might be due to Requests not using proper HTTP method when handling redirects. The bug was fixed on 30-03-2012; see issue #269 (fix subsequent redirect request method type) for more details.

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366