tornado tcpclient 应用实例

来源:互联网 发布:2017年旅游数据 编辑:程序博客网 时间:2024/06/09 15:27

公司项目要求,在tornado的框架下,后端要求使用tcp跟其它程序进行通信,查看了tornad tcpclient 源码后写了一个实例,供大家参考,关键位置都已经给出了,稍微修改就可以运行,注:非生产代码,只提供一个样例.

https://github.com/tornadoweb/tornado/blob/master/tornado/tcpclient.py代码注释的非常详细可以直接阅读,懒得翻译了。


class TestTcpclient(object):    """docstring for TestTcpClient"""    def __init__(self, host,port):        self.host = host        self.port = port    @gen.coroutine    def start(self):        self.stream = yield TCPClient().connect(self.host, self.port)        self.stream.write('hello')        rec=yield self.stream.read_until('/n')        print 'recive from the server',recdef test_main():    tcp_client = TestTcpclient('host', 'port')    tcp_client.start()    IOLoop.instance().start()


0 0
原创粉丝点击