__negotiatehttp(self,destaddr,destport) Negotiates a connection through an HTTP server.
(self, destaddr, destport)
| 328 | self.__proxypeername = (destaddr, destport) |
| 329 | |
| 330 | def __negotiatehttp(self, destaddr, destport): |
| 331 | """__negotiatehttp(self,destaddr,destport) |
| 332 | Negotiates a connection through an HTTP server. |
| 333 | """ |
| 334 | # If we need to resolve locally, we do this now |
| 335 | if not self.__proxy[3]: |
| 336 | addr = socket.gethostbyname(destaddr) |
| 337 | else: |
| 338 | addr = destaddr |
| 339 | self.sendall(("CONNECT " + addr + ":" + str(destport) + " HTTP/1.1\r\n" + "Host: " + destaddr + "\r\n\r\n").encode()) |
| 340 | # We read the response until we get the string "\r\n\r\n" |
| 341 | resp = self.recv(1) |
| 342 | while resp.find("\r\n\r\n".encode()) == -1: |
| 343 | resp = resp + self.recv(1) |
| 344 | # We just need the first line to check if the connection |
| 345 | # was successful |
| 346 | statusline = resp.splitlines()[0].split(" ".encode(), 2) |
| 347 | if statusline[0] not in ("HTTP/1.0".encode(), "HTTP/1.1".encode()): |
| 348 | self.close() |
| 349 | raise GeneralProxyError((1, _generalerrors[1])) |
| 350 | try: |
| 351 | statuscode = int(statusline[1]) |
| 352 | except ValueError: |
| 353 | self.close() |
| 354 | raise GeneralProxyError((1, _generalerrors[1])) |
| 355 | if statuscode != 200: |
| 356 | self.close() |
| 357 | raise HTTPError((statuscode, statusline[2])) |
| 358 | self.__proxysockname = ("0.0.0.0", 0) |
| 359 | self.__proxypeername = (addr, destport) |
| 360 | |
| 361 | def connect(self, destpair): |
| 362 | """connect(self, despair) |