From Test-Scratch-Wiki
(Redirected from Eng:Communicating to Scratch via Python 3.x)
Please expand this article or section. You can help by adding more information if you are an editor. More information might be found in a section of the talk page. |
This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines. |
This page has links to outside of the Scratch website or Wikipedia. Remember to stay safe when using the internet as we can't guarantee the safety of other sites. |
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