1

我正在使用requests library嘗試在phpBB論壇上發帖。Python請求庫沒有正確跟蹤302?

我的代碼看起來像下面這樣:

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) 

然後我檢查是否主題是通過搜索響應字符串成功發佈。

流量的流量是,在第一篇文章之後,有一個302重定向,請求隨後會到達成功發佈到的頁面。

有時它可以工作(可能是50次中的1次),但通常不起作用。響應只是第一個獲取請求的頁面。 (帖子後的回覆頁面)

它應該重定向到張貼的消息幾乎每次,但它不出於某種原因。

如果我用wireshark嗅探流量,看起來響應通常是通過URL重定向到的,但似乎在那裏出了問題。

任何人都知道什麼可能會導致這首先失敗,以及如何解決它?

回答

3

我認爲你的問題可能是由於請求在處理重定向時不使用正確的HTTP方法。該錯誤在30-03-2012確定;請參閱問題#269(修復後續重定向請求方法類型)以獲取更多詳細信息。