How to connect a Python socket on client-side to Node.js/socket.io?

Deadly 提交于 2019-12-21 02:46:10

问题


I want to connect Blender (v2.55) to a webpage through sockets.

For the web part, I can use Node.js & socket.io. I've already used a little node.js/socket.io, it's not a problem I think.

Now for Blender, it runs on Python 3.1, so I've already sockets and I can add libraries if needed. I'm new to Python sockets, can I connect a client to node.js/socket.io directly ?

I tried with the basic code from the Python doc:


import socket
import sys

HOST, PORT = "127.0.0.1", 8080
data = "Hello from Blender"

# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to server and send data
sock.connect((HOST, PORT))
sock.send(bytes(data + "\n","utf8"))

# Receive data from the server and shut down
received = sock.recv(1024)
sock.close()

print("Sent:     %s" % data)
print("Received: %s" % received)

It results by:

Sent:     Hello from Blender
Received: b''

It seems that Blender is connected, but doesn't receive data. Also Node shows no new client connected…

Do I need something else ? If somebody can help me out…


回答1:


You are missing a protocol/handshake. What you have there is a bare TCP socket connection. node.js/socket.io lives on top of a TCP socket. Basically when you open a connection to a socket.io server, it's expecting you to use some protocol for communication (websockets, longpolling, htmlfile, whatever). The initial handshake defines what that protocol will be. Websockets is one of the supported protocols. This blog post should help you. It doesn't look all that hard to get websockets implemented.



来源:https://stackoverflow.com/questions/4086435/how-to-connect-a-python-socket-on-client-side-to-node-js-socket-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!