您可能会发现这很有用,请记住HTTPResponse不是设计为“由用户直接实例化”
还要注意,响应字符串中的内容长度头可能不再有效(这取决于获取这些响应的方式),这只是意味着对HTTPResponse.read()的调用需要具有大于内容的值才能获得全部内容。
在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')
pr