askcompu

Untitled

Jul 29th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1.     def initiate_connect(self, host, port, use_ssl, ipv6):
  2.         if self.verbose:
  3.             message = 'Connecting to %s:%s...' % (host, port)
  4.             print(message, end=' ', file=sys.stderr)
  5.         if ipv6 and socket.has_ipv6:
  6.              af = socket.AF_INET6
  7.         else:
  8.              af = socket.AF_INET
  9.         self.create_socket(af, socket.SOCK_STREAM, use_ssl)
  10.         try:
  11.             self.connect((host, port))
  12.         except socket.error:
  13.             if socket.error.errno == errno.ETIMEDOUT:
  14.                 if self.verbose:
  15.                     print("Connection timed out, retrying in 10 seconds", file=sys.stderr)
  16.                 time.sleep(10.0)
  17.                 initiate_connect(host, port, use_ssl, ipv6)
  18.             elif socket.error.errno == errno.ECONNRESET:
  19.                 if self.verbose:
  20.                     print("Connection reset by peer, retrying in 10 seconds", file=sys.stderr)
  21.                 time.sleep(10.0)
  22.                 initiate_connect(host, port, use_ssl, ipv6)
  23.             else:
  24.                 raise
  25.         try: asyncore.loop()
  26.         except KeyboardInterrupt:
  27.             sys.exit()
  28.  
  29.     def create_socket(self, family, type, use_ssl=False):
  30.         self.family_and_type = family, type
  31.         sock = socket.socket(family, type)
  32.         sock.settimeout(120.0)
  33.         if use_ssl:
  34.             #try:
  35.                # sock = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1,
  36.                #     cert_reqs=ssl.CERT_OPTIONAL, ca_certs=self.ca_certs)
  37.             #except ssl.SSLError:
  38.             context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
  39.             context.verify_mode = ssl.CERT_NONE
  40.             context.load_verify_locations(cafile = self.ca_certs, capath = '/etc/ssl/certs/ca-certificates.crt')
  41.             sock = context.wrap_socket(sock)
  42.         # FIXME: ssl module does not appear to work properly with nonblocking sockets
  43.         #sock.setblocking(0)
  44.         self.set_socket(sock)
Advertisement
Add Comment
Please, Sign In to add comment