| 1 | import time |
| 2 | from socket import socket, error, AF_INET, SOCK_STREAM |
| 3 | |
| 4 | |
| 5 | def wait_for_socket(server_name, host, port, timeout=2): |
| 6 | ok = False |
| 7 | for x in range(10): |
| 8 | try: |
| 9 | print('Testing [%s] proxy server on %s:%d' |
| 10 | % (server_name, host, port)) |
| 11 | s = socket(AF_INET, SOCK_STREAM) |
| 12 | s.connect((host, port)) |
| 13 | s.close() |
| 14 | except error as ex: |
| 15 | print('ERROR', ex) |
| 16 | time.sleep(timeout/10.0) |
| 17 | else: |
| 18 | print('Connection established') |
| 19 | ok = True |
| 20 | break |
| 21 | if not ok: |
| 22 | raise Exception('The %s proxy server has not started in %d seconds' |
| 23 | % (server_name, timeout)) |