chunked_request.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:lddmm-ot 作者: jeanfeydy 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号