python:socketio-client无法正常工作

时间:2017-07-19 13:53:50

标签: python-2.7

我编写了这个使用socketio客户端模块的python代码。

 from socketIO_client import SocketIO

    print "connecting to server"
    socketIO = SocketIO('localhost', 8888, transports=['websocket'])
    print "Connected"

    def sendSocketId():
        socketIO.emit('authenticate_python', "Python is connected")

    def socketDisconnect():   
        socketIO.disconnect()

    def doSomething(data):
        print "message from ui : : ", data
        socketIO.emit("msg_from_python","Message from python : : Hi! " + data)

    try:
        print socketIO.connected
        if socketIO.connected:
            sendSocketId()
        socketIO.on('msg_from_node', doSomething)
        socketIO.wait()
    except Exception as e:
        print "Exception : : ", e
        socketDisconnect()

但是当我连接到服务器并运行此文件时,它会引发以下错误:

Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
connecting to server

Traceback (most recent call last):
  File "C:\Users\student\app.py", line 4, in <module>
    socketIO = SocketIO('localhost', 8888, transports=['websocket'])
  File "C:\Python27\lib\site-packages\socketIO_client\__init__.py", line 353, in __init__
    resource, hurry_interval_in_seconds, **kw)
  File "C:\Python27\lib\site-packages\socketIO_client\__init__.py", line 54, in __init__
    self._transport
  File "C:\Python27\lib\site-packages\socketIO_client\__init__.py", line 62, in _transport
    self._engineIO_session = self._get_engineIO_session()
  File "C:\Python27\lib\site-packages\socketIO_client\__init__.py", line 76, in _get_engineIO_session
    transport.recv_packet())
StopIteration
>>> 

我对此错误一无所知,也没有关于如何使用此socketio客户端模块的正确文档。

2 个答案:

答案 0 :(得分:2)

通过卸载socket.io 2.0.0版本并安装它的watever版本与socketIO-client版本匹配来解决该问题。

答案 1 :(得分:0)

此模块socketIO-client-nexus修复了兼容的套接字版本socket.io协议1.x的问题

https://pypi.org/project/socketIO-client-nexus/0.7.6/

不向后兼容

如果要使用socket.io协议0.9进行通信,请使用soketIO-client 0.5.7.2

https://pypi.org/project/socketIO-client/0.5.7.2/

别忘了从导入socketIO-client更改为socketIO-client-nexus