4

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…

3
  • Can you connect to "127.0.0.1", 8080 using telnet or netcat (like telnet 127.0.0.1 8080) and send/receive data through telnet? You python script looks ok. Commented Nov 3, 2010 at 11:21
  • It's connected but there is no notification on Node side… I used this code before with a Python socket-server, it worked pretty well… Commented Nov 3, 2010 at 11:41
  • Ok, so it seems that I've to look at UDP sockets, both for Python and Node.js… Commented Nov 3, 2010 at 13:42

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

There also appears to be a Python Websocket Client Library now. I have not used it - github.com/mtah/python-websocket
I'm trying the python-websocket but I have another problem, I posted about it here: stackoverflow.com/questions/4101621/…
It seems that the blog is down :(
0

you can try the form of loop to receive valid data.

    import socket
    host="127.0.0.1"
    port=8088
    web=socket.socket()
    web.bind((host,port))
    web.listen(5)
    print("recycle")
    while True:
        conn,addr=web.accept()
        data=conn.recv(8)
        print(data)
        conn.sendall(b'HTTP/1.1 200 OK\r\n\r\nHello world')
        conn.close()

and use your browser to visit the host and port for a check

Comments

0

I understand this thread is extremely old. But I faced the same problem recently and couldn't find an answer or any similar questions. So here is my answer.

Answer: Use socket.io for python python-socketio

The reason why built-in sockets or any other websocket library in python won't work is explained in the socket.io website socket.io Socketio is simply just not a websoket connection. Although they say, it uses websockets for transport internally, the connection is established with HTTP protocol http:// as opposed to the WEBSOCKET protocol ws://. This results in the failure of handshake and the connection fails to be established.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.