2012-07-07 127 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