pyList

基于tornado.websocket 的websocket 客户端

一个简单的基于tornado.websocket 的websocket 客户端

#!/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)

运行:

python xx.py

输出:

{"body": "foo"}
本文网址: https://pylist.com/t/1545894022 (转载注明出处)
如果你有任何建议或疑问可以在下面 留言
Be the first to comment!
Captcha image
Relative Articles