Eng:Communicating to Scratch via Python

Here are some instructions on how to use Python 3.x to communicate with Scratch via Remote Sensor Connections. These instructions will create a minimal program that sends commands.

Install Python

 * Install Python
 * Mac OS X has Python pre-installed. It can be run from the command line.
 * The latest release version can be obtained from here.
 * In your scratch project, right-click on the "sensor value" block and enable remote sensor connections.

Sample Program
Open a text editor and create a new file. Save it as communication.py (you can call it whatever you like), and put this code in it: import socket

HOST = 'localhost' PORT = 42001

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT))

def sendCMD(cmd): s.send(len(cmd).to_bytes(4, 'big')) s.send(bytes(cmd,'UTF-8'))

sendCMD('broadcast "hello"') Then run it with Python. You can do this from the command line: python communication.py