connects to a host/port
(host, port)
| 488 | |
| 489 | |
| 490 | def start_client(host, port): |
| 491 | """connects to a host/port""" |
| 492 | pydev_log.info("Connecting to %s:%s", host, port) |
| 493 | |
| 494 | address_family = AF_INET |
| 495 | for res in socket_module.getaddrinfo(host, port, 0, SOCK_STREAM): |
| 496 | if res[0] == AF_INET: |
| 497 | address_family = res[0] |
| 498 | # Prefer IPv4 addresses for backward compat. |
| 499 | break |
| 500 | if res[0] == AF_INET6: |
| 501 | # Don't break after this - if the socket is dual-stack prefer IPv4. |
| 502 | address_family = res[0] |
| 503 | |
| 504 | s = socket(address_family, SOCK_STREAM) |
| 505 | |
| 506 | # Set TCP keepalive on an open socket. |
| 507 | # It activates after 1 second (TCP_KEEPIDLE,) of idleness, |
| 508 | # then sends a keepalive ping once every 3 seconds (TCP_KEEPINTVL), |
| 509 | # and closes the connection after 5 failed ping (TCP_KEEPCNT), or 15 seconds |
| 510 | try: |
| 511 | s.setsockopt(SOL_SOCKET, socket_module.SO_KEEPALIVE, 1) |
| 512 | except (AttributeError, OSError): |
| 513 | pass # May not be available everywhere. |
| 514 | try: |
| 515 | s.setsockopt(socket_module.IPPROTO_TCP, socket_module.TCP_KEEPIDLE, 1) |
| 516 | except (AttributeError, OSError): |
| 517 | pass # May not be available everywhere. |
| 518 | try: |
| 519 | s.setsockopt(socket_module.IPPROTO_TCP, socket_module.TCP_KEEPINTVL, 3) |
| 520 | except (AttributeError, OSError): |
| 521 | pass # May not be available everywhere. |
| 522 | try: |
| 523 | s.setsockopt(socket_module.IPPROTO_TCP, socket_module.TCP_KEEPCNT, 5) |
| 524 | except (AttributeError, OSError): |
| 525 | pass # May not be available everywhere. |
| 526 | |
| 527 | try: |
| 528 | # 10 seconds default timeout |
| 529 | timeout = int(os.environ.get("PYDEVD_CONNECT_TIMEOUT", 10)) |
| 530 | s.settimeout(timeout) |
| 531 | s.connect((host, port)) |
| 532 | s.settimeout(None) # no timeout after connected |
| 533 | pydev_log.info(f"Connected to: {s}.") |
| 534 | return s |
| 535 | except: |
| 536 | pydev_log.exception("Could not connect to %s: %s", host, port) |
| 537 | raise |
| 538 | |
| 539 | |
| 540 | INTERNAL_TERMINATE_THREAD = 1 |