public
Last active

Connect to socket.io server from python, using websocket

  • Download Gist
client.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#!/usr/bin/env python
# encoding: utf-8
"""
client.py
 
Based heavily on:
- https://github.com/mtah/python-websocket/blob/master/examples/echo.py
- http://stackoverflow.com/a/7586302/316044
 
Created by Drew Harry on 2011-11-28.
"""
 
import websocket, httplib, sys, asyncore
 
'''
connect to the socketio server
 
1. perform the HTTP handshake
2. open a websocket connection '''
def connect(server, port):
print("connecting to: %s:%d" %(server, port))
conn = httplib.HTTPConnection(server + ":" + str(port))
conn.request('POST','/socket.io/1/')
resp = conn.getresponse()
hskey = resp.read().split(':')[0]
 
ws = websocket.WebSocket(
'ws://'+server+':'+str(port)+'/socket.io/1/websocket/'+hskey,
onopen = _onopen,
onmessage = _onmessage,
onclose = _onclose)
return ws
 
def _onopen():
print("opened!")
 
def _onmessage(msg):
print("msg: " + str(msg))
 
def _onclose():
print("closed!")
 
 
if __name__ == '__main__':
if len(sys.argv) != 3:
sys.stderr.write('usage: python client.py <server> <port>\n')
sys.exit(1)
server = sys.argv[1]
port = int(sys.argv[2])
ws = connect(server, port)
try:
asyncore.loop()
except KeyboardInterrupt:
ws.close()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.