[英]urllib2 - post request
我嘗試用urllib2執行一個簡單的POST請求。 但是服務器響應表明它收到一個簡單的GET。 我檢查了傳出請求的類型,但它設置為POST。
為了檢查服務器是否像我期望的那樣,我嘗試使用連接到url的(以前的POST-)數據執行GET請求。 這給了我預期的答案。
有人知道我誤解了什么嗎?
def connect(self):
url = 'http://www.mitfahrgelegenheit.de/mitfahrzentrale/Dresden/Potsdam.html/'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
header = { 'User-Agent' : user_agent }
values = {
'city_from' : 69,
'radius_from' : 0,
'city_to' : 263,
'radius_to' : 0,
'date' : 'date',
'day' : 5,
'month' : 03,
'year' : 2012,
'tolerance' : 0
}
data = urllib.urlencode(values)
# req = urllib2.Request(url+data, None, header) # GET works fine
req = urllib2.Request(url, data, header) # POST request doesn't not work
self.response = urllib2.urlopen(req)
這似乎是一個類似於這里討論的問題: Python URLLib / URLLib2 POST但我很確定在我的情況下,不會丟失尾部斜杠。 ;)
我擔心這可能是一個愚蠢的誤解,但我已經好幾個小時了!
編輯:打印的便利功能:
def response_to_str(response):
return response.read()
def dump_response_to_file(response):
f = open('dump.html','w')
f.write(response_to_str(response))
編輯2:決議:
我找到了一個工具來捕捉與網站的真實互動, http://fiddler2.com/fiddler2/ 。 顯然,服務器從輸入表單中獲取數據,重定向幾次然后發出GET請求,並將此數據簡單地附加到url。
urllib2一切都很好,我為濫用你的時間而道歉!
您可以使用Firefox的Firebug或Google Chromes DevTools找到正確的帖子URL。
我為您提供了一些支持cookie的代碼,以便您可以先登錄並使用cookie通過您的帖子參數發出后續請求。
最后,如果您可以向我們展示一些示例HTML輸出,那將使生活更輕松。
這里的是我的代碼已經對我很可靠至今就職於POST-ING於大多數網頁包括受保護的頁面CSRF / XSRF只要你能夠正確地找出張貼在哪里 (哪個URL()張貼到)。
import cookielib
import socket
import urllib
import urllib2
url = 'http://www.mitfahrgelegenheit.de/mitfahrzentrale/Dresden/Potsdam.html/'
http_header = {
"User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11",
"Accept" : "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,text/png,*/*;q=0.5",
"Accept-Language" : "en-us,en;q=0.5",
"Accept-Charset" : "ISO-8859-1",
"Content-type": "application/x-www-form-urlencoded",
"Host" : "www.mitfahrgelegenheit.de",
"Referer" : "http://www.mitfahrgelegenheit.de/mitfahrzentrale/Dresden/Potsdam.html/"
}
params = {
'city_from' : 169,
'radius_from' : 0,
'city_to' : 263,
'radius_to' : 0,
'date' : 'date',
'day' : 5,
'month' : 03,
'year' : 2012,
'tolerance' : 0
}
# setup socket connection timeout
timeout = 15
socket.setdefaulttimeout(timeout)
# setup cookie handler
cookie_jar = cookielib.LWPCookieJar()
cookie = urllib2.HTTPCookieProcessor(cookie_jar)
# setup proxy handler, in case some-day you need to use a proxy server
proxy = {} # example: {"http" : "www.blah.com:8080"}
# create an urllib2 opener()
#opener = urllib2.build_opener(proxy, cookie) # with proxy
opener = urllib2.build_opener(cookie) # we are not going to use proxy now
# create your HTTP request
req = urllib2.Request(url, urllib.urlencode(params), http_header)
# submit your request
res = opener.open(req)
html = res.read()
# save retrieved HTML to file
open("tmp.html", "w").write(html)
print html
只是為了結束這個問題:
問題確實是,服務器沒有期望POST請求(盡管應該考慮用例)。 所以(再次)框架沒有被打破。 ;)
嘗試將標題添加到標題中:
'Content-type': 'application/x-www-form-urlencoded'
嘗試從URL中刪除尾部斜杠,如下所示:
url = 'http://www.mitfahrgelegenheit.de/mitfahrzentrale/Dresden/Potsdam.html'
可能是您發送POST
請求的服務器腳本實際上不支持POST
請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.