util.py 718 B

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