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?

Community
  • 1
  • 1
Thinkwell
  • 285
  • 1
  • 5
  • 16

3 Answers3

2

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

  • 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. – Thinkwell Feb 18 '15 at 21:56
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.

Ilker Kesen
  • 483
  • 6
  • 11
  • 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. – Thinkwell Feb 21 '15 at 02:52
1

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

Ben Darnell
  • 21,844
  • 3
  • 29
  • 50
  • 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? – Thinkwell Feb 18 '15 at 21:54
  • Hmm, we do need better docs and examples for this. Right now the best examples are in the tests: https://github.com/tornadoweb/tornado/blob/6932b7384adb75cf505d59cfed5c13868d1439a4/tornado/test/websocket_test.py#L131-136 – Ben Darnell Feb 18 '15 at 22:07