Neozaru
Neozaru

Reputation: 1130

High level Python library for using Websocket and Comet "fallback" transparently

I'm looking for a High Level Python library for establishing HTTP connections to a Web server. The connections should ideally remain open (persistant) for sending and receiving two-ways messages, so Websocket are great to me. As I want it to be compatible will most HTTP proxies, I think about a "fallback" mode with HTTP polling (Comet style).

My problem is I can't find a library for managing these two kinds of connections transparently. Ideally, I would establish the connection to the server with one of the techniques (Websocket or Comet), then simply send/receive messages using the same functions for both types of connections.

I found many Python servers and some Js clients for that purpose, but not in Python. I looked at : Twisted, Tornado, ZeroMQ, py4ws

Upvotes: 1

Views: 1129

Answers (2)

Neozaru
Neozaru

Reputation: 1130

I think Python Socket-IO client could be a good solution : https://github.com/invisibleroads/socketIO-client

He can interact easily with a Socket.io NodeJs server, with same paradigms.

I tested it and it can default with Websocket connection and fallbacks to xhr-polling, which is great (I actually tested this features through a proxy).

Example :

with SocketIO('http://127.0.0.1', 7777, Namespace, transports=["websocket", "xhr-polling"], proxies={'http': 'http://localhost:8888'}) as socketIO:
    socketIO.on('foo',some_callback_function)
    socketIO.emit('bar')
    socketIO.wait()

Upvotes: 0

msvalkon
msvalkon

Reputation: 12077

Have you taken a look at socket.io? It mainly works with websockets but has plenty of fallbacks and is thus supposed to be supported by all browsers.

For the server side, I've used flask together with gevent-socketio. Miquel Gringberg has recently released flask-socketio extension which is a nice abstraction for working with flask and gevent-socketio. Gevent-socketio is built on the nice gevent library.

gevent-socketio should work fine with other Python frameworks, such as Django and Bottle.

I'm not entirely sure if this fits your bill but probably worth a look.

Upvotes: 1

Related Questions