2

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?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

SHAZZAM!! I was able to get it going with your example. Perhaps I should have on the earlier posts too, but I didn't. Thanks everyone for your assistance.
2

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

1 Comment

The tornado-websocket-client looks great, except that I can't figure out how to use it. I tried instantating as the example class shows, ws = HelloSocket('ws://echo.websocket.org') ws.connect() and then using ws.write_message(msg), but it doesn't send to the server. Can't figure out what I'm doing wrong. I did get websocket-client to work...so I'm out of my jam, but I was hoping for something a bit lighter-weight.
1

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

2 Comments

I did come across that in the docs, but can't figure out how to use it. Haven't encountered any examples in the wild. How do you instantiate the client and then use the connection in a forloop? That's the question?
Hmm, we do need better docs and examples for this. Right now the best examples are in the tests: github.com/tornadoweb/tornado/blob/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.