First of all, I'm not sure why some of your Socket.IO servers won't support websockets...the intent of Socket.IO is to make front-end browser development of web apps easier by providing an abstracted interface to real-time data streams being served up by the Socket.IO server. Perhaps Socket.IO is not what you should be using for your application? That aside, let me try to answer your question...
At this point in time, there aren't any Socket.IO client libraries for Python (gevent-socketio is not a Socket.IO client library for Python...it is a Socket.IO server library for Python). For now, you are going to have to piece some original code together in order to interface with Socket.IO directly as a client while accepting various connection types.
I know you are looking for a cure-all that works across various connection types (WebSocket, long-polling, etc.), but since a library such as this does not exist as of yet, I can at least give you some guidance on using the WebSocket connection type based on my experience.
For the WebSocket connection type, create a WebSocket client in Python. From the command line install this Python WebSocket Client package here with pip so that it is on your python path like so:
pip install -e git+https://github.com/liris/websocket-client.git#egg=websocket
Once you've done that try the following, replacing SOCKET_IO_HOST
and SOCKET_IO_PORT
with the appropriate location of your Socket.IO server:
import websocket
SOCKET_IO_HOST = "127.0.0.1"
SOCKET_IO_PORT = 8080
socket_io_url = 'ws://' + SOCKET_IO_HOST + ':' + str(SOCKET_IO_PORT) + '/socket.io/websocket'
ws = websocket.create_connection(socket_io_url)
At this point you have a medium of interfacing with a Socket.IO server directly from Python. To send messages to the Socket.IO server simply send a message through this WebSocket connection. In order for the Socket.IO server to properly interpret incoming messages through this WebSocket from your Python Socket.IO client, you need to adhere to the Socket.IO protocol and encode any strings or dictionaries you might send through the WebSocket connection. For example, after you've accomplished everything above do the following:
def encode_for_socketio(message):
"""
Encode 'message' string or dictionary to be able
to be transported via a Python WebSocket client to
a Socket.IO server (which is capable of receiving
WebSocket communications). This method taken from
gevent-socketio.
"""
MSG_FRAME = "~m~"
HEARTBEAT_FRAME = "~h~"
JSON_FRAME = "~j~"
if isinstance(message, basestring):
encoded_msg = message
elif isinstance(message, (object, dict)):
return encode_for_socketio(JSON_FRAME + json.dumps(message))
else:
raise ValueError("Can't encode message.")
return MSG_FRAME + str(len(encoded_msg)) + MSG_FRAME + encoded_msg
msg = "Hello, world!"
msg = encode_for_socketio(msg)
ws.send(msg)