Esempio n. 1
0
def get_socket_http_response(sock):
    charset = 'utf-8'
    _UNKNOWN = 'UNKNOWN'
    http_response = None
    # 接收html字节数据
    r = client.HTTPResponse(sock)
    try:
        try:
            r.begin()
        except ConnectionError as ce:
            logger.error('拉取数据异常:%s', ce)
        will_close = r.will_close
        http_response = HTTPResponse.from_httplib(r)
        if will_close and will_close != _UNKNOWN:
            # logger.debug('数据已接收,主机关闭了连接')
            sock.close()
    except Exception as e:
        logger.error('数据接收异常:%s', e)
    finally:
        r.close()
        # print('response:')
        # print(response.decode(charset))
        # 保持连接
        if http_response is not None:
            setattr(http_response, "body", http_response.data.decode(charset))
            return http_response
        else:
            return None
Esempio n. 2
0
def _response_from_bytes(data: bytes) -> Urllib3HTTPResponse:
    class BytesIOSocket:
        def __init__(self, content):
            self.handle = BytesIO(content)

        def makefile(self, mode) -> BytesIO:
            return self.handle

    sock = BytesIOSocket(data)

    response = HttpHTTPResponse(sock)
    response.begin()

    return Urllib3HTTPResponse.from_httplib(response)