我想解析http响应并获取所有状态代码。
使用的命令
curl -i -X PUT -T test1.txt http://sandbox.hortonworks.com:50075/webhdfs/v1/user/test.txt?op=CREATE&namenoderpcaddress=sandbox.hortonworks.com:8020&overwrite=false?op=CREATE
以下是我正在尝试解析的回复
HTTP/1.1 100 Continue
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 173
Connection: close
这是我正在使用的代码
rom httplib import HTTPResponse
from StringIO import StringIO
http_response_str = """HTTP/1.1 100 Continue
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 173
Connection: close"""
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))
此代码为我提供状态代码:101但我需要响应中的状态代码。 有人可以建议吗?