File: utils.py

package info (click to toggle)
python-socks 2.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544 kB
  • sloc: python: 5,191; sh: 8; makefile: 3
file content (24 lines) | stat: -rw-r--r-- 593 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import socket
import time


def is_connectable(host, port):
    try:
        sock = socket.create_connection((host, port), 1)
    except socket.error:
        return False
    else:
        sock.close()
        return True


def wait_until_connectable(host, port, timeout=10):
    count = 0
    while not is_connectable(host=host, port=port):
        if count >= timeout:
            raise Exception(
                f'The proxy server has not available by ({host}, {port}) in {timeout:d} seconds'
            )
        count += 1
        time.sleep(1)
    return True