def _getresponse(self):
''' Read from recv and return a HTTPResponse object if possible.
Either
1 - The client has succesfully closed the connection: Return ''
2 - The server has already closed the connection: Return the response
if possible.
'''
# Wait for a response
self._conn.sock.setblocking(True)
# Parse the response
response = self._bytes
while True:
try:
_bytes = self._conn.sock.recv(1)
except http_client.socket.error:
# For error 54: Connection reset by peer
# (and perhaps others)
return six.b('')
if _bytes == six.b(''):
break
else:
response += _bytes
# Set recv to be non-blocking again
self._conn.sock.setblocking(False)
# Convert the response string to a http_client.HTTPResponse
# object with a bit of a hack
if response != six.b(''):
# Taken from
# http://pythonwise.blogspot.ca/2010/02/parse-http-response.html
try:
response = http_client.HTTPResponse(_FakeSocket(response))
response.begin()
except:
# Bad headers ... etc.
response = six.b('')
return response
评论列表
文章目录