def acquire_network_resources(self): """ Configure a network connection to the server. The resulting socket is handled by two instances of ServerConnectionReader and ServerConnectionWriter, which are created and run as well. It is possibile to send/receive message to/from them through the queues self.output_message_queue and self._input_queue (servermessage). Note: this private method should be actually "protected" for the ServerSession states, but Python doesn't have such a protection level. """ try: self.logger.debug(u"Creating a socket on %s:%s", self.host, self.port) sock = socket.create_connection((self.host, self.port), timeout=10) ca_chain = os.path.abspath(self.server_certificate) self.sock = ssl.wrap_socket(sock, cert_reqs=ssl.CERT_REQUIRED, ca_certs=ca_chain, ssl_version=ssl.PROTOCOL_TLSv1) self.sock.setblocking(True) match_hostname(self.sock.getpeercert(), self.host) except CertificateError as e: self.logger.critical(u"SSL certificate validation failed: %s" % e) raise ssl.SSLError(e) except socket.error as exception: self.logger.debug(u"Error opening SSL socket: %s" % exception) self.logger.warning( u"Unable to connect, re-trying in %s seconds." % self.reconnection_time) self.reconnection_time = stoppable_exponential_backoff_waiting( self.reconnection_time, self.must_die, 10) self.num_connection_attempts += 1 return False except socket.timeout as exception: self.logger.debug(u"Socket timeout: %s" % exception) self.logger.warning( u"Unable to connect, re-trying in %s seconds." % self.reconnection_time) self.reconnection_time = stoppable_exponential_backoff_waiting( self.reconnection_time, self.must_die, 10) self.num_connection_attempts += 1 return False self.connection_reader = ServerConnectionReader( self._input_queue, self.input_keepalive_queue, self.sock) self.connection_writer = ServerConnectionWriter( self._input_queue, self.output_message_queue, self.sock) self.connection_reader.start() self.connection_writer.start() self.reconnection_time = 1 return True