def testClientSocket(self): sock = pulsar.create_client_socket(('', 8080)) self.assertFalse(sock.is_server) self.assertTrue(sock.name in (None, ('0.0.0.0', 0))) sock2 = pulsar.create_client_socket('0.0.0.0:8080') self.assertFalse(sock2.is_server) self.assertTrue(sock2.name in (None, ('0.0.0.0', 0)))
def testRepr(self): sock = pulsar.create_client_socket(':0') self.assertTrue(repr(sock)) self.assertTrue(sock.info().startswith('client ')) fd = sock.fileno() state = sock.__getstate__() self.assertEqual(fd, state['fd'])
def connect(self, address): """Connects the socket to a remote address without blocking. May only be called if the socket passed to the constructor was not available or it was not previously connected. The address parameter is in the same format as for socket.connect, i.e. a (host, port) tuple or a string for unix sockets. If callback is specified, it will be called when the connection is completed. Note that it is safe to call IOStream.write while the connection is pending, in which case the data will be written as soon as the connection is ready. Calling IOStream read methods before the socket is connected works on some platforms but is non-portable.""" if self._state is None and not self.connecting: if self.sock is None: self.sock = create_client_socket(address) try: self.sock.connect(address) except socket.error as e: # In non-blocking mode connect() always raises an exception if not async_error(e): LOGGER.warning('Connect error on %s: %s', self, e) self.close() return callback = Deferred(description='%s connect callback' % self) self._connect_callback = callback self._add_io_state(self.WRITE) return callback else: raise RuntimeError('Cannot connect. State is %s.' % self.state_code)