2012-07-07 68 views
2

我想僅使用原始字符串發送POST請求。Python:僅使用原始字符串發送POST請求

我在寫解析器。我已經加載頁面,並在Firebug看到許多頭部和身體這麼複雜的要求:

__EVENTTARGET=&__EVENTARGUMENT=&__VIEW.... (11Kb or unreadable text) 

我怎樣才能發送此確切的請求一次(頭+後體)手動(把它當作一個巨大的字符串)?

像:

func("%(headers) \n \n %(body)" % ...) 

我希望由我腳本發送(和響應進行處理),不想做的參數和頭的字典手動。

謝謝。

+0

你怎麼知道標題和身體是什麼? – 2012-07-07 15:39:58

+0

@BurhanKhalid,我從螢火蟲複製它。我要解析迴應。 – 2012-07-07 15:45:29

+0

我不明白你正在努力實現的過程的細節。您是否問如何使用python的原始字符串發送POST請求?當你在談論螢火蟲時,它開始扔我,就好像你想做的事情是客戶端 – jdi 2012-07-07 15:55:48

回答

7

另一個答案太大了,混淆不清,顯示的比你要求的更多。我覺得我應該有一個更簡潔的答案以供將來讀者會遇到:

import urllib2 
import urllib 
import urlparse 

# this was the header and data strings you already had 
headers = 'baz=3&foo=1&bar=2' 
data = 'baz=3&foo=1&bar=2' 

header_dict = dict(urlparse.parse_qsl(headers)) 

r = urllib2.Request('http://www.foo.com', data, headers) 
resp = urllib2.urlopen(r) 

您將需要至少解析頭回的字典,但其最小的工作。然後把它傳遞給一個新的請求。

*注意:這個簡潔的例子假設您的標題和數據正文都是application/x-www-form-urlencoded格式。如果標題的格式爲Key: Value等原始字符串格式,請參閱其他答案以獲取有關首先解析的更多詳細信息。

最終,您不能複製粘貼原始文本並運行新的請求。它必須以適當的格式分成標題和數據。

+0

'urllib2.HTTPError:HTTP Error 400:Bad Request' with [this](http://pastebin.com/JtyGfcEa)[this](http://www.sberbank-ast.ru/purchaseList。 aspx)頁面查詢「Названиеаукциона」=福特。你有任何想法,爲什麼它可能是? – 2012-07-07 16:47:38

+0

你真的已經分開你的頭和你的身體,他們都'應用程序/ x-www-form-urlencoded'嗎? – jdi 2012-07-07 16:51:44

+0

我的後身是錯的。現在它可以工作。但有時服務器的答案錯誤的編碼(我只能看到垃圾在我的輸出中),儘管事實頭是「Accept-Charset」:'utf-8; q = 0.7,*; q = 0.3''。無論如何,謝謝。 – 2012-07-08 17:49:22

1
import urllib 
import urllib2 

# DATA: 

# option #1 - using a dictionary 
values = {'name': 'Michael Foord', 'location': 'Northampton', 'language': 'Python' } 
data = urllib.urlencode(values) 

# option #2 - directly as a string 
data = 'name=Michael+Foord&language=Python&location=Northampton' 

# HEADERS: 

# option #1 - convert a bulk of headers to a dictionary (really, don't do this)  

headers = ''' 
Host: www.http.header.free.fr 
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, 
Accept-Language: Fr 
Accept-Encoding: gzip, deflate 
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0) 
Connection: Keep-Alive 
''' 

headers = dict([[field.strip() for field in pair.split(':', 1)] for pair in headers.strip().split('\n')]) 

# option #2 - just use a dictionary 

headers = {'Accept': 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,', 
      'Accept-Encoding': 'gzip, deflate', 
      'Accept-Language': 'Fr', 
      'Connection': 'Keep-Alive', 
      'Host': 'www.http.header.free.fr', 
      'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)'} 

# send the request and receive the response 

req = urllib2.Request('http://www.someserver.com/cgi-bin/register.cgi', data, headers) 
response = urllib2.urlopen(req) 
the_page = response.read() 
+0

看起來,即使沒有標題,它也能以某種方式工作。但我最好把它們轉換成字典。謝謝你,@ jdi。 – 2012-07-07 16:14:45

+0

@Minner:'urlparse.parse_qsl(headers)' – jdi 2012-07-07 16:22:45

+0

你的頭文件示例假設OP已經有字典。也許從我的'urlparse'建議開始,並將其傳遞給標題值,而不是所有那個大標題解析東西 – jdi 2012-07-07 16:25:22