def connect(self, server):
"""Creates a (ssl) socket and connects to the server. Not using asyncore's connect-function because it sucks."""
# sockets are garbage collected, but if the connection isn't closed it might fail
try:
self.socket.shutdown(socket.SHUT_WR)
self.socket.close()
del self.socket
except Exception:
pass
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
if self.use_ssl:
try:
self.socket.setblocking(True)
self.socket = ssl.wrap_socket(self.socket)
except (ssl.SSLWantReadError, ssl.SSLWantWriteError) as e:
log.debug(e)
self._handshake()
except ssl.SSLError as e:
log.error(e)
self.exit()
return
finally:
self.socket.setblocking(False)
log.info('Connecting to %s', self.current_server)
self.socket.settimeout(30)
self.socket.connect(server)
self.handle_connect_event()
评论列表
文章目录