简体   繁体   中英

Python websocket client - sending messages from python code to WebSocketServer

I need to send system logs to the browser and so I have a tornado-based websocket server running like so.

class WSHandler(tornado.websocket.WebSocketHandler):

    def check_origin(self, origin):
        return True

    def get(self, *args, **kwargs):
        self.ip = self.get_argument('ip', None)
        self.action = self.get_argument('action', None)
        super(WSHandler, self).get(self, *args, **kwargs)

    def open(self, *args, **kwargs):

        clients.append(self)

    def on_message(self, message):

        ll = eval(message)

        for cl in clients:
            if cl.ip and cl.ip != ll.user:
                continue
            if cl.action and cl.action != ll.action:
                continue

            message = '%s %s' % (ll.action, ll.url)
            cl.write_message(message)

    def on_close(self):
        try:
            clients.remove(self)
        except ValueError:
            pass

The examples I've encountered so far revolve around Tornado-based servers and js-based clients.

What I need, however, is an easy way to connect to this websocket from a Python client, preferably powered by Tornado. The client does not need to receive messages - only send them. I thought I had my answer with this SO post,

How to run functions outside websocket loop in python (tornado)

...but I need to send a message whenever a log event occurs, and preferably from my code that's parsing the events. The examples I've encountered so far revolve around Tornado-based servers and js-based clients. Is there a short & sweet tornado-based client that only sends messages, that can be called from a for-loop?

There is tornad-websocket-client project. Pay attention on it.
Also there is simple websocket-client to just send messages.

Also, I developed a complete Tornado WebSocket Client/Server example.

https://github.com/ilkerkesen/tornado-websocket-client-example

If you want WebSocket Authentication/Authorization, look at my other projects trebol and sugar .

Tornado包含一个websocket客户端: http : //www.tornadoweb.org/en/stable/websocket.html#client-side-support

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM