python解析http响应(字符串)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24728088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python parse http response (string)
提问by abdus_salam
I'm using python 2.7 and I want to parse string HTTP response fields which I already extracted from a text file. What would be the easiest way? I can parse requests by using the BaseHTTPServer but couldn't manage to find something for the responses.
我正在使用 python 2.7,我想解析我已经从文本文件中提取的字符串 HTTP 响应字段。什么是最简单的方法?我可以使用 BaseHTTPServer 解析请求,但无法找到响应的内容。
The responses I have are pretty standard and in the following format
我的回答非常标准,格式如下
HTTP/1.1 200 OK
Date: Thu, Jul 3 15:27:54 2014
Content-Type: text/xml; charset="utf-8"
Connection: close
Content-Length: 626
Thanks in advance,
提前致谢,
采纳答案by Jeremy Allen
You might find this useful, keep in mind that HTTPResponsewasn't designed to be "instantiated directly by user."
您可能会发现这很有用,请记住HTTPResponse并非旨在“由用户直接实例化”。
Also note that the content-length header in your response string may not be valid any more (it depends on how you've aquired these responses) this just means that the call to HTTPResponse.read() needs to have value larger than the content in order to get it all.
另请注意,响应字符串中的内容长度标头可能不再有效(这取决于您如何获取这些响应)这仅意味着对 HTTPResponse.read() 的调用需要具有大于内容的值为了得到这一切。
In python 2 it can be run this way.
在python 2中,它可以这样运行。
from httplib import HTTPResponse
from StringIO import StringIO
http_response_str = """HTTP/1.1 200 OK
Date: Thu, Jul 3 15:27:54 2014
Content-Type: text/xml; charset="utf-8"
Connection: close
Content-Length: 626"""
class FakeSocket():
def __init__(self, response_str):
self._file = StringIO(response_str)
def makefile(self, *args, **kwargs):
return self._file
source = FakeSocket(http_response_str)
response = HTTPResponse(source)
response.begin()
print "status:", response.status
print "single header:", response.getheader('Content-Type')
print "content:", response.read(len(http_response_str)) # the len here will give a 'big enough' value to read the whole content
In python 3, the HTTPResponse
is imported from http.client
, and the response to be parsed needs to be byte encoded. Depending on where the data is gotten from this may be done already or need to be called explicitly
在python 3中,HTTPResponse
是从 导入的http.client
,要解析的响应需要进行字节编码。取决于从中获取数据的位置,这可能已经完成或需要显式调用
from http.client import HTTPResponse
from io import BytesIO
http_response_str = """HTTP/1.1 200 OK
Date: Thu, Jul 3 15:27:54 2014
Content-Type: text/xml; charset="utf-8"
Connection: close
Content-Length: 626
teststring"""
http_response_bytes = http_response_str.encode()
class FakeSocket():
def __init__(self, response_bytes):
self._file = BytesIO(response_bytes)
def makefile(self, *args, **kwargs):
return self._file
source = FakeSocket(http_response_bytes)
response = HTTPResponse(source)
response.begin()
print( "status:", response.status)
# status: 200
print( "single header:", response.getheader('Content-Type'))
# single header: text/xml; charset="utf-8"
print( "content:", response.read(len(http_response_str)))
# content: b'teststring'
回答by Hitesh Dharamdasani
You might want to consider using python-requests.
您可能需要考虑使用 python-requests。
Link: http://docs.python-requests.org/en/latest/
链接:http: //docs.python-requests.org/en/latest/
Here is an example from http://dancallahan.info/journal/python-requests/
这是来自http://dancallahan.info/journal/python-requests/的示例
Considering your responses are compliant with HTTP RFC
考虑到您的响应符合 HTTP RFC
Does this look like something you want to do?
这看起来像你想做的事情吗?
>>> import requests
>>> url = 'http://example.test/'
>>> response = requests.get(url)
>>> response.status_code
200
>>> response.headers['content-type']
'text/html; charset=utf-8'
>>> response.content
u'Hello, world!'