一个简单的基于 tornado.websocket 的 websocket 客户端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from tornado.ioloop import IOLoop
from tornado import gen
from tornado.websocket import websocket_connect
@gen.coroutine
def main():
client = yield websocket_connect('ws://echo.websocket.org')
client.write_message(json.dumps({'body': 'foo'}))
message = yield client.read_message()
print(message)
if __name__ == "__main__":
IOLoop.current().run_sync(main)
运行:
1
python xx.py
输出:
1
{"body": "foo"}
本文网址: https://pylist.com/topic/168.html 转摘请注明来源