Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:仅使用原始字符串发送POST请求_Python_Post_Custom Action - Fatal编程技术网

Python:仅使用原始字符串发送POST请求

Python:仅使用原始字符串发送POST请求,python,post,custom-action,Python,Post,Custom Action,我想只使用一个原始字符串发送一个POST请求 我正在写一个解析器。我已加载页面,并在firebug中看到了如此复杂的请求,其中包含许多标题和正文: __EVENTTARGET=&__EVENTARGUMENT=&__VIEW.... (11Kb or unreadable text) 我怎样才能再次手动发送这个确切的请求(标题+帖子正文)(将其作为一个巨大的字符串传递) 比如: 我希望它通过我的脚本发送(并处理响应),而不希望手动创建参数和头的字典 谢谢。另一个答案太大了,太让

我想只使用一个原始字符串发送一个POST请求

我正在写一个解析器。我已加载页面,并在firebug中看到了如此复杂的请求,其中包含许多标题和正文:

__EVENTTARGET=&__EVENTARGUMENT=&__VIEW.... (11Kb or unreadable text)
我怎样才能再次手动发送这个确切的请求(标题+帖子正文)(将其作为一个巨大的字符串传递)

比如:

我希望它通过我的脚本发送(并处理响应),而不希望手动创建参数和头的字典


谢谢。

另一个答案太大了,太让人困惑了,而且比你要问的要多。我觉得我应该为未来的读者提供一个更简洁的答案:

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()
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)
您至少需要将头解析回dict,但这是最简单的工作。然后把它一直传递给一个新的请求

*注意:这个简明的示例假设您的头和数据体都是
application/x-www-form-urlencoded
格式。如果标题是原始字符串格式,如
Key:Value
,那么请参阅另一个答案以了解有关首先解析该标题的更多详细信息


最终,您不能只是复制粘贴原始文本并运行新请求。它必须以正确的格式分为标题和数据。

你怎么知道标题是什么和主体是什么?@Burnkhalid,我从firebug复制了它。我将解析响应。我不理解您试图实现的过程的细节。您是否在询问如何仅使用python中的原始字符串发送POST请求?当你谈论萤火虫的时候,我开始感到困惑,好像你想在客户端做点什么do@jdi,是的,只是一个使用python原始字符串的请求。谢谢你的措辞。你可能别无选择,但至少要将标题转换为dict,以便将其添加到请求中。似乎即使没有标题,它也能以某种方式工作。但我最好把它们译成字典。谢谢,@jdi@MInner:
urlparse.parse_qsl(headers)
您的头示例假设OP已经有了字典。也许从我的
urlparse
建议开始,然后将其传递给header值,而不是将所有大的header解析内容添加到帖子中。您只需将标题作为字典传递即可。另一种方法是将它们转换为字典,或者使用套接字并自己实现HTTP协议的一部分。我不相信这是您想要的。
urllib2.HTTPError:HTTP Error 400:Bad Request
查询“khззззаааааааааааа。你知道为什么吗?你真的已经把你的标题和正文分开了吗?它们都是
application/x-www-form-urlencoded
?我的帖子正文错了。现在它起作用了。但有时服务器应答时使用错误的编码(我只能在输出中看到垃圾),尽管事实上头是
'Accept-Charset':'utf-8;q=0.7,*;q=0.3'
。无论如何,谢谢你。
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)