First, let me express my gratitude to the Stack Overflow community - although I haven't posted before, I've found countless solutions to my problems in the past. I greatly appreciate the time and energy that the community invests in making this an excellent resource for everyone.
I'm trying to fetch data from a server which uses Socket.IO in conjunction with XHR-polling data. Although I can seemingly connect with the Python script, I can't properly receive data.
I've looked at the outgoing and incoming packets in Fiddler: every ~5 seconds, the browser receives useful information (i.e. '5:::{"name":"pollData","args":["<..'), but the Python script receives NOOP responses ('8::').
Problematic part of code:
reply = requests.get(URL + "/socket.io/1/?t=" + str(long(time.time()*1000)), headers=headers)
session_id = reply.text.split(':')[0]
print "Session ID:", session_id
reply = requests.post(URL + "/socket.io/1/xhr-polling/" + session_id + "?t=" +
str(long(time.time()*1000)), data=message, headers=headers)
print "Successfully subscribed."
for i in range(5):
sleep(5)
reply = requests.get(URL + "/socket.io/1/xhr-polling/" + session_id + "?t=" +
str(long(time.time()*1000)), headers=headers)
print reply.text
I've tried using the Websocket-client, but it was generating this error: WebSocketException: Handshake Status 200
SocketIO-client produces this error: SocketIOError: Could not establish connection
More importantly than using another library to solve this problem, I'd like to understand why this is happening. The outgoing packets produced by the script and by Chrome look, at least to a layman, basically identical - why are they producing such different results?
(Please feel welcome to ask any questions or request additional information.)
Just in case it benefits someone in the future, after much trial and error I realized that it needed one more GET request before it could POST the subscription information. Some of the code below might be superfluous, but it seems to work:
reply = requests.get(url + "?t=" + get_timecode(), stream=True)
session_id = reply.text.split(':')[0]
main = requests.Session()
print "Session ID:", session_id
reply = main.get(url + "xhr-polling/" + session_id + "?t=" + get_timecode(),
stream=True)
reply = main.post(url + "xhr-polling/" + session_id + "?t=" + get_timecode(),
data=subscribe, stream=True)
print "Successfully subscribed."
while 1:
reply = main.get(url + "xhr-polling/" + session_id + "?t=" + get_timecode(),
stream=True)
[Note that this was only necessary because the server's websocket was broken and Socket.IO had to fallback on XHR-polling. Also, there are probably much easier ways to do this.]