Thinkwell
Thinkwell

Reputation: 285

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?

Upvotes: 2

Views: 10337

Answers (3)

Ilker Kesen
Ilker Kesen

Reputation: 513

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.

Upvotes: 2

Ben Darnell
Ben Darnell

Reputation: 22134

Tornado includes a websocket client: http://www.tornadoweb.org/en/stable/websocket.html#client-side-support

Upvotes: 1

user4579288
user4579288

Reputation:

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

Upvotes: 2

Related Questions