(self)
| 921 | return ip |
| 922 | |
| 923 | def _tunnel(self): |
| 924 | connect = b"CONNECT %s:%d HTTP/1.0\r\n" % ( |
| 925 | self._wrap_ipv6(self._tunnel_host.encode("ascii")), |
| 926 | self._tunnel_port) |
| 927 | headers = [connect] |
| 928 | for header, value in self._tunnel_headers.items(): |
| 929 | headers.append(f"{header}: {value}\r\n".encode("latin-1")) |
| 930 | headers.append(b"\r\n") |
| 931 | # Making a single send() call instead of one per line encourages |
| 932 | # the host OS to use a more optimal packet size instead of |
| 933 | # potentially emitting a series of small packets. |
| 934 | self.send(b"".join(headers)) |
| 935 | del headers |
| 936 | |
| 937 | response = self.response_class(self.sock, method=self._method) |
| 938 | try: |
| 939 | (version, code, message) = response._read_status() |
| 940 | |
| 941 | if code != http.HTTPStatus.OK: |
| 942 | self.close() |
| 943 | raise OSError(f"Tunnel connection failed: {code} {message.strip()}") |
| 944 | while True: |
| 945 | line = response.fp.readline(_MAXLINE + 1) |
| 946 | if len(line) > _MAXLINE: |
| 947 | raise LineTooLong("header line") |
| 948 | if not line: |
| 949 | # for sites which EOF without sending a trailer |
| 950 | break |
| 951 | if line in (b'\r\n', b'\n', b''): |
| 952 | break |
| 953 | |
| 954 | if self.debuglevel > 0: |
| 955 | print('header:', line.decode()) |
| 956 | finally: |
| 957 | response.close() |
| 958 | |
| 959 | def connect(self): |
| 960 | """Connect to the host and port specified in __init__.""" |
no test coverage detected