Python中文
首页
教程
问答
标签
搜索
登录
注册
从python clien格式化要发送到socket.io node.js服务器的消息
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我试图让一个Python客户机使用Socket.io 0.7与Node.js服务器对话,方法是向服务器发送一个自定义事件。</p> <p>基于我在<a href="https://github.com/LearnBoost/socket.io-spec" rel="nofollow noreferrer">GitHub</a>和下面的<a href="https://github.com/liris/websocket-client" rel="nofollow noreferrer">WebSocket Python library</a>上找到的Socket.io引用。</p> <p>这是我目前的代码:</p> <p><strong>节点服务器</strong></p> <pre><code>io.sockets.on('connection', function (socket) { socket.on('newimg', function(data) { console.log(data); }); }); </code></pre> <p><strong>Python客户端</strong></p> <pre><code>def handshake(host, port): u = urlopen("http://%s:%d/socket.io/1/" % (host, port)) if u.getcode() == 200: response = u.readline() (sid, hbtimeout, ctimeout, supported) = response.split(":") supportedlist = supported.split(",") if "websocket" in supportedlist: return (sid, hbtimeout, ctimeout) else: raise TransportException() else: raise InvalidResponseException() try: (sid, hbtimeout, ctimeout) = handshake(HOSTNAME, PORT) #handshaking according to socket.io spec. Except Exception as e: print e sys.exit(1) ws = websocket.create_connection("ws://%s:%d/socket.io/1/websocket/%s" % (HOSTNAME, PORT, sid)) print ws.recv() ws.send("2::") ws.send("5:1::{'name':'newimg', 'args':'bla'}") print ws.recv() print "Closing connection" ws.close() </code></pre> <p><strong>节点控制台输出</p> <pre><code>debug - client authorized info - handshake authorized 12738935571241622933 debug - setting request GET /socket.io/1/websocket/12738935571241622933 debug - set heartbeat interval for client 12738935571241622933 debug - client authorized for debug - websocket writing 1:: debug - websocket received data packet 2:: debug - got heartbeat packet debug - websocket received data packet 5:1::{'name':'newimg', 'args':'bla'} debug - acknowledging packet automatically debug - websocket writing 6:::1 info - transport end debug - set close timeout for client 12738935571241622933 debug - cleared close timeout for client 12738935571241622933 debug - cleared heartbeat interval for client 12738935571241622933 debug - discarding transport </code></pre> <p><strong>Python控制台输出</p> <pre><code>Done 1:: 6:::1 Closing connection </code></pre> <p>现在看来,尽管服务器使用ACK响应,套接字事件似乎没有被触发。因此,消息被正确接收,但我假设,没有正确格式化socket.io以触发事件。</p> <p>我不认为框架是必要的,但是Archie1986似乎不同意他对此的回答:<a href="https://stackoverflow.com/questions/4762086/socket-io-client-library-in-python">Socket.IO Client Library in Python</a></p> <p>我在这里会做错什么?</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>解决了。我需要用双引号。单引号不是有效的JSON。胡说。</p> <pre><code>ws.send("5:1::{'name':'newimg', 'args':'bla'}") </code></pre> <p>变成:</p> <pre><code>ws.send('5:1::{"name":"newimg", "args":"bla"}') </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
将Pandas数据帧转换为PyTorch张量?
6 回答
将Pandas数据帧转换为scipy稀疏矩阵
1 回答
将Pandas数据帧转换为Spark Datafram时出现问题
4 回答
将pandas数据帧转换为spark DataFram时出错
6 回答
将Pandas数据帧转换为spark datafram时收到错误
4 回答
将Pandas数据帧转换为Spark数据帧
8 回答
将Pandas数据帧转换为Tensorflow数据
7 回答
将Pandas数据帧转换为tkinter obj
6 回答
将pandas数据帧转换为XML
6 回答
将Pandas数据帧转换为值sql语句
2 回答
将pandas数据帧转换为元组
5 回答
将pandas数据帧转换为元组列表
2 回答
将pandas数据帧转换为元组列表并删除所有pandas数据类型
7 回答
将pandas数据帧转换为具有头和数据类型的numpy数组
6 回答
将pandas数据帧转换为内存中的拼花地板,并将其加载到Python中的Hadoop中
6 回答
将pandas数据帧转换为内存中类似文件的对象?
7 回答
将Pandas数据帧转换为内存功能(&F)
4 回答
将pandas数据帧转换为列表列表
5 回答
将pandas数据帧转换为列表列表以输入到RNN
5 回答
将Pandas数据帧转换为单行DataFram
8 回答