本文整理汇总了Python中tornado.tcpclient.TCPClient.connect方法的典型用法代码示例。如果您正苦于以下问题:Python TCPClient.connect方法的具体用法?Python TCPClient.connect怎么用?Python TCPClient.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.tcpclient.TCPClient
的用法示例。
在下文中一共展示了TCPClient.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _run_traffic_jam
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def _run_traffic_jam(nsends, nbytes):
# This test eats `nsends * nbytes` bytes in RAM
np = pytest.importorskip('numpy')
from distributed.protocol import to_serialize
data = bytes(np.random.randint(0, 255, size=(nbytes,)).astype('u1').data)
with echo_server() as e:
client = TCPClient()
stream = yield client.connect('127.0.0.1', e.port)
b = BatchedSend(interval=0.01)
b.start(stream)
msg = {'x': to_serialize(data)}
for i in range(nsends):
b.send(assoc(msg, 'i', i))
if np.random.random() > 0.5:
yield gen.sleep(0.001)
results = []
count = 0
while len(results) < nsends:
# If this times out then I think it's a backpressure issue
# Somehow we're able to flood the socket so that the receiving end
# loses some of our messages
L = yield gen.with_timeout(timedelta(seconds=5), read(stream))
count += 1
results.extend(r['i'] for r in L)
assert count == b.batch_count == e.count
assert b.message_count == nsends
assert results == list(range(nsends))
stream.close() # external closing
yield b.close(ignore_closed=True)
示例2: test_stress
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def test_stress():
with echo_server() as e:
client = TCPClient()
stream = yield client.connect('127.0.0.1', e.port)
L = []
@gen.coroutine
def send():
b = BatchedSend(interval=3)
b.start(stream)
for i in range(0, 10000, 2):
b.send(i)
b.send(i + 1)
yield gen.sleep(0.00001 * random.randint(1, 10))
@gen.coroutine
def recv():
while True:
result = yield gen.with_timeout(timedelta(seconds=1), read(stream))
print(result)
L.extend(result)
if result[-1] == 9999:
break
yield All([send(), recv()])
assert L == list(range(0, 10000, 1))
stream.close()
示例3: connect
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def connect(self, address, deserialize=True, **connection_args):
self._check_encryption(address, connection_args)
ip, port = parse_host_port(address)
kwargs = self._get_connect_args(**connection_args)
client = TCPClient()
try:
stream = yield client.connect(ip, port,
max_buffer_size=MAX_BUFFER_SIZE,
**kwargs)
# Under certain circumstances tornado will have a closed connnection with an error and not raise
# a StreamClosedError.
#
# This occurs with tornado 5.x and openssl 1.1+
if stream.closed() and stream.error:
raise StreamClosedError(stream.error)
except StreamClosedError as e:
# The socket connect() call failed
convert_stream_closed_error(self, e)
local_address = self.prefix + get_stream_address(stream)
raise gen.Return(self.comm_class(stream,
local_address,
self.prefix + address,
deserialize))
示例4: connect
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def connect(self):
client = TCPClient(io_loop=self.io_loop)
self.stream = yield client.connect(self.host, self.port)
# sock = None
# try:
# if self.unix_socket and self.host in ('localhost', '127.0.0.1'):
# sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# t = sock.gettimeout()
# sock.settimeout(self.connect_timeout)
# sock.connect(self.unix_socket)
# sock.settimeout(t)
# self.host_info = "Localhost via UNIX socket"
# if DEBUG: print('connected using unix_socket')
# else:
# while True:
# try:
# sock = socket.create_connection(
# (self.host, self.port), self.connect_timeout)
# break
# except (OSError, IOError) as e:
# if e.errno == errno.EINTR:
# continue
# raise
# self.host_info = "socket %s:%d" % (self.host, self.port)
# if DEBUG: print('connected using socket')
# sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
# if self.no_delay:
# sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
# self.socket = sock
# self._rfile = _makefile(sock, 'rb')
yield self._get_server_information()
yield self._request_authentication()
示例5: test_close_twice
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def test_close_twice():
with echo_server() as e:
client = TCPClient()
stream = yield client.connect('127.0.0.1', e.port)
b = BatchedSend(interval=10)
b.start(stream)
yield b.close()
yield b.close()
示例6: test_send_after_stream_finish
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def test_send_after_stream_finish():
with echo_server() as e:
client = TCPClient()
stream = yield client.connect('127.0.0.1', e.port)
b = BatchedSend(interval=10)
b.start(stream)
yield b.last_send
b.send('hello')
result = yield read(stream); assert result == ['hello']
示例7: test_close_closed
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def test_close_closed():
with echo_server() as e:
client = TCPClient()
stream = yield client.connect('127.0.0.1', e.port)
b = BatchedSend(interval=10)
b.start(stream)
b.send(123)
stream.close() # external closing
yield b.close(ignore_closed=True)
示例8: connect
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def connect(self, host, port):
self.host = host
self.port = port
client = TCPClient()
try:
self.stream = yield client.connect(self.host, self.port)
except IOError as e:
log.error("%s", repr(e))
raise gen.Return((False, 'Failed to connect'))
self.trigger(Event.CONNECT, self)
raise gen.Return((True, "OK"))
示例9: test_send_before_start
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def test_send_before_start():
with echo_server() as e:
client = TCPClient()
stream = yield client.connect('127.0.0.1', e.port)
b = BatchedSend(interval=10)
b.send('hello')
b.send('world')
b.start(stream)
result = yield read(stream); assert result == ['hello', 'world']
示例10: connect
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def connect(ip, port, timeout=1):
client = TCPClient()
start = time()
while True:
try:
stream = yield client.connect(ip, port)
raise Return(stream)
except StreamClosedError:
if time() - start < timeout:
yield gen.sleep(0.01)
logger.debug("sleeping on connect")
else:
raise
示例11: __init__
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
class SafeTcpStream:
def __init__(self, host, port):
self.host = host
self.port = port
self.stream = None
self.tcp_client = TCPClient()
self.make_tcp_connection_loop()
@gen.coroutine
def make_tcp_connection_loop(self, once=False):
while True:
if self.stream is None:
try:
if once:
self.stream = yield gen.with_timeout(
ioloop.IOLoop.instance().time() + ONCE_CONNECT_TIMEOUT,
self.tcp_client.connect(self.host, self.port))
else:
self.stream = yield self.tcp_client.connect(self.host, self.port)
if self.stream is not None:
self.stream.set_close_callback(self.disconnected)
except Exception:
pass
if once:
break
yield gen.Task(ioloop.IOLoop.instance().add_timeout, ioloop.IOLoop.instance().time() + RECONNECT_INTERVAL)
def disconnected(self):
try:
self.stream.close()
except Exception:
pass
self.stream = None
def write(self, data):
if self.stream is not None:
return self.stream.write(data)
示例12: start_app
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def start_app():
tcpClient = TCPClient()
try:
stream = yield tcpClient.connect('127.0.0.1', 9999)
print 'Connection started'
app = LaternController(LanternDriver())
client = TLVClient(stream)
executer = ThreadPoolExecutor(max_workers=5)
while True:
command = yield client.getCommand()
executer.submit(app.handle, command)
except Exception as e:
print 'Caught Error: %s' % e
IOLoop.instance().add_callback(IOLoop.instance().stop)
示例13: test_BatchedStream_raises
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def test_BatchedStream_raises():
port = 3435
server = MyServer()
server.listen(port)
client = TCPClient()
stream = yield client.connect('127.0.0.1', port)
b = BatchedStream(stream, interval=20)
stream.close()
with pytest.raises(StreamClosedError):
yield b.recv()
with pytest.raises(StreamClosedError):
yield b.send('123')
示例14: connect
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def connect(ip, port, timeout=3):
client = TCPClient()
start = time()
while True:
try:
future = client.connect(ip, port, max_buffer_size=MAX_BUFFER_SIZE)
stream = yield gen.with_timeout(timedelta(seconds=timeout), future)
raise Return(stream)
except StreamClosedError:
if time() - start < timeout:
yield gen.sleep(0.01)
logger.debug("sleeping on connect")
else:
raise
except gen.TimeoutError:
raise IOError("Timed out while connecting to %s:%d" % (ip, port))
示例15: test_BatchedStream
# 需要导入模块: from tornado.tcpclient import TCPClient [as 别名]
# 或者: from tornado.tcpclient.TCPClient import connect [as 别名]
def test_BatchedStream():
port = 3434
server = MyServer()
server.listen(port)
client = TCPClient()
stream = yield client.connect('127.0.0.1', port)
b = BatchedStream(stream, interval=20)
b.send('hello')
b.send('world')
result = yield b.recv(); assert result == 'hello'
result = yield b.recv(); assert result == 'hello'
result = yield b.recv(); assert result == 'world'
result = yield b.recv(); assert result == 'world'
b.close()