Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def initiate_connect(self, host, port, use_ssl, ipv6):
- if self.verbose:
- message = 'Connecting to %s:%s...' % (host, port)
- print(message, end=' ', file=sys.stderr)
- if ipv6 and socket.has_ipv6:
- af = socket.AF_INET6
- else:
- af = socket.AF_INET
- self.create_socket(af, socket.SOCK_STREAM, use_ssl)
- try:
- self.connect((host, port))
- except socket.error:
- if socket.error.errno == errno.ETIMEDOUT:
- if self.verbose:
- print("Connection timed out, retrying in 10 seconds", file=sys.stderr)
- time.sleep(10.0)
- initiate_connect(host, port, use_ssl, ipv6)
- elif socket.error.errno == errno.ECONNRESET:
- if self.verbose:
- print("Connection reset by peer, retrying in 10 seconds", file=sys.stderr)
- time.sleep(10.0)
- initiate_connect(host, port, use_ssl, ipv6)
- else:
- raise
- try: asyncore.loop()
- except KeyboardInterrupt:
- sys.exit()
- def create_socket(self, family, type, use_ssl=False):
- self.family_and_type = family, type
- sock = socket.socket(family, type)
- sock.settimeout(120.0)
- if use_ssl:
- #try:
- # sock = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1,
- # cert_reqs=ssl.CERT_OPTIONAL, ca_certs=self.ca_certs)
- #except ssl.SSLError:
- context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
- context.verify_mode = ssl.CERT_NONE
- context.load_verify_locations(cafile = self.ca_certs, capath = '/etc/ssl/certs/ca-certificates.crt')
- sock = context.wrap_socket(sock)
- # FIXME: ssl module does not appear to work properly with nonblocking sockets
- #sock.setblocking(0)
- self.set_socket(sock)
Advertisement
Add Comment
Please, Sign In to add comment