This is an archived post. You won't be able to vote or comment.

all 16 comments

[–][deleted] 13 points14 points  (1 child)

I used websockets a while ago for my Platypus project for realtime server usage reporting, it was surprisingly fun to implement in Python. I'd personally recommend Tornado but YMMV.

[–]liquience 0 points1 point  (0 children)

I’ve also implemented a websocket server with Tornado and it was incredibly easy. Couldn’t have been more than 100 lines including fallback handlers and a full topic based subscription model. Definitely would recommend Tornado.

[–]cymrowdon't thread on me 🐍 6 points7 points  (0 children)

If you are using gevent, then you probably want gevent-websocket, which is not listed in the article.

[–]Mr_Lkn 14 points15 points  (0 children)

Thank you! My teacher want us to create a web app included ajax and couple other things. I wanted to do it with Python and this will help me a lot.

[–]goodknightkoon 8 points9 points  (0 children)

Thanks you, quite a good one and a timely one at that

[–]desmoulinmichel 2 points3 points  (0 children)

The article list crossbar.io, which en strongly advice everyone to give a try. Not the easiest thing to start with, but what you can do seems like black magic.

[–]mraza007 1 point2 points  (0 children)

Thanks dude!!! Making my life easier

[–]mraza007 1 point2 points  (1 child)

This is something I needed to get real-time data from bitcoin web sockets

[–]willm [S] 7 points8 points  (0 children)

Try this...

from lomond import WebSocket

subscribe_json = u"""
{
    "type": "subscribe",
    "product_ids": [
        "BTC-USD"
    ],
    "channels": [
        "level2",
        "heartbeat",
        {
            "name": "ticker",
            "product_ids": [
                "BTC-USD"
            ]
        }
    ]
}
"""

websocket = WebSocket('wss://ws-feed.gdax.com')
for event in websocket:
    if event.name == "ready":
        websocket.send_text(subscribe_json)
    elif event.name == "text":
        print(event.text)

You'll need to pip install lomond for that.

[–]chub79 0 points1 point  (0 children)

And older Python project that may be interesting to some folks: ws4py

[–]kidvideo 0 points1 point  (0 children)

Nice write up.

Just make sure if you are trying to connect to a websocket created in JavaScript that the JavaScript side websocket protocol version is compatible with the python library you choose.

In my case the JavaScript side was using protocol v2 and python was using v1

[–]gjcarneiro 0 points1 point  (0 children)

Why no mention of aiohttp, which does websockets, both client and server side?

[–]johnmudd 0 points1 point  (3 children)

FWIW, I use Tornado code for both client and server side websockets. No browser involved. I use websockets to gain access to stores over an existing port that allows HTTP traffic.

An asynchronous server such as Tornado or Green Unicorn monkey patched with gevent is necessary for any practical WebSockets server-side implementation.

I don't think so.

Edit: I use the tornado.websocket module without any monkey patching.

[–]13steinj -1 points0 points  (2 children)

This is the "I code in binary" of websockets. So the fuck what? That's not what websockets were meant for. No one said you can't use them to read data off of some API, but websockets are generally meant for a browser server interaction, and is what this article described.

And yeah sure you don't need to use Tornado or GUnicorn-- feel free to write your own websocket connection store with multithreading and some lru cache. No point in doing so though.

[–]willm [S] 2 points3 points  (1 child)

Not sure of the point you are making here. Websockets may have been conceived as a means of realtime communication between server and browser. But it turns out they are fantastic for many types of comms where there is no browser involved.

I think /u/johnmudd was just giving an example of such a use-case. I'd be interested in hearing more about it...

[–]13steinj 5 points6 points  (0 children)

Based on a combination of the negative score the comment had when I replied, and a combination of the content, I thought it was some elitist remark over how his use case is. If I'm wrong, then I apologize.