def _dump_response_data_only(response, prefixes, bytearr, n=0): prefix = prefixes.response # Let's interact almost entirely with urllib3's response raw = response.raw # Let's convert the version int from httplib to bytes version_str = HTTP_VERSIONS.get(raw.version, b'?') # <prefix>HTTP/<version_str> <status_code> <reason> bytearr.extend(prefix + b'HTTP/' + version_str + b' ' + str(raw.status).encode('ascii') + b' ' + _coerce_to_bytes(response.reason) + b'\r\n') headers = raw.headers for name in headers.keys(): for value in headers.getlist(name): bytearr.extend(prefix + _format_header(name, value)) bytearr.extend(prefix + b'\r\n') bytearr.extend(response.content) t = str(int(time.time())) print(type(t), t) # print("type---111",bytearr.decode("utf-8")) with open(str(n) + "_s.txt", "wb") as f: f.write(bytearr) print("nnnnnnnnnnnnnnnnnnnnnnnnnnnn", n)
def _dump_request_data_only(request, prefixes, bytearr, proxy_info=None, n=0): if proxy_info is None: proxy_info = {} prefix = prefixes.request method = _coerce_to_bytes(proxy_info.pop('method', request.method)) request_path, uri = _build_request_path(request.url, proxy_info) # <prefix><METHOD> <request-path> HTTP/1.1 bytearr.extend(prefix + method + b'' + request_path + b' HTTP/1.1\r\n') # <prefix>Host: <request-host> OR host header specified by user headers = request.headers.copy() host_header = _coerce_to_bytes(headers.pop('Host', uri.netloc)) bytearr.extend(prefix + b'Host: ' + host_header + b'\r\n') for name, value in headers.items(): bytearr.extend(prefix + _format_header(name, value)) bytearr.extend(prefix + b'\r\n') if request.body: if isinstance(request.body, compat.basestring): bytearr.extend(prefix + _coerce_to_bytes(request.body)) else: bytearr.extend(b'<< Request body is not a string-like type >>') bytearr.extend(b'\r\n') t = str(int(time.time())) print(type(t), t) txt = bytearr.decode("utf-8") print(type(txt)) with open(str(n) + "_c.txt", "wb") as f: f.write(bytearr)
def test_format_header_handles_unicode(self): """Prove that _format_header correctly formats text input.""" header = b'Connection'.decode('utf-8') value = b'close'.decode('utf-8') expected = b'Connection: close\r\n' assert dump._format_header(header, value) == expected
def test_format_header(self): """Prove that _format_header correctly formats bytes input.""" header = b'Connection' value = b'close' expected = b'Connection: close\r\n' assert dump._format_header(header, value) == expected