Send data from Python to Processing via socket communication

Overview

When prototyping easily, you can easily create it by leaving complicated processing to Python and drawing to Processing. When I was looking for a good way to cooperate, I could simply make it with socket communication. Please let me know if there is a better way.

code

Below is a sample code that only sends a text message unilaterally from Python to Processing. If you start the Processing side first and then execute the Python side, a message will be output to the standard output of Processing.

Processing (receiver)

conenctWithPython.pde


import processing.net.*;

int port = 10001; //Set an appropriate port number

Server server;

void setup() {
  server = new Server(this, port);
  println("server address: " + server.ip()); //Output IP address
}

void draw() {
  Client client = server.available();
  if (client !=null) {
    String whatClientSaid = client.readString();
    if (whatClientSaid != null) {
      println(whatClientSaid); //Output message from Python
    } 
  } 
}

Python (sender)

toProcessing.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket

host = "127.0.0.1" #IP address of the server launched by Processing
port = 10001       #Port number set in Processing

if __name__ == '__main__':
    socket_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Creating an object
    socket_client.connect((host, port))                               #Connect to server

    #socket_client.send('Message to send')                #Send data Python2
    socket_client.send('Message to send'.encode('utf-8')) #Send data Python3

Recommended Posts

Send data from Python to Processing via socket communication
Socket communication and multi-thread processing by Python
Send and receive binary data via serial communication with python3 (on mac)
Send a message from Slack to a Python server
[Python] How to read data from CIFAR-10 and CIFAR-100
[Python] Flow from web scraping to data analysis
Python Socket communication sample / simple data throwing tool
Changes from Python 3.0 to Python 3.5
(Miscellaneous notes) Data update pattern from CSV data acquisition / processing to Excel by Python
POST messages from python to Slack via incoming webhook
How to scrape image data from flickr with python
Send log data from the server to Splunk Cloud
Get data from database via ODBC with Python (Access)
Post from Python to Slack
Cheating from PHP to Python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Introduction to serial communication [Python]
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
I want to send a message from Python to LINE Bot
Hit REST in Python to get data from New Relic
Meteorology x Python ~ From weather data acquisition to spectrum analysis ~
Pass OpenCV data from the original C ++ library to Python
Call Matlab from Python to optimize
From Elasticsearch installation to data entry
Ported from R language of "Sazae-san's rock-paper-scissors data analysis" to Python
[Python] How to FFT mp3 data
Leave the troublesome processing to Python
Socket communication with Python LEGO Mindstorms
Python: Exclude tags from html data
processing to use notMNIST data in Python (and tried to classify it)
Hit treasure data from Python Pandas
Create folders from '01' to '12' with python
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
How to avoid duplication of data when inputting from Python to SQLite.
Introduction to Data Analysis with Python P17-P26 [ch02 1.usa.gov data from bit.ly]
Connect to utf8mb4 database from python
Python (from first time to execution)
Post images from Python to Tumblr
Send commands from Atom to Maya
Python3 socket module and socket communication flow
How to access wikipedia from python
Python to switch from another language
[Python] Create API to send Gmail
Send email via gmail with Python 3.4.3.
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
I tried to communicate with a remote server by Socket communication with Python.
How to send a visualization image of data created in Python to Typetalk
Data integration from Python app on Linux to Amazon Redshift with ODBC
Transfer floating point data from Python to JavaScript without loss of digits
Data integration from Python app on Windows to Amazon Redshift with ODBC
Copy data from Amazon S3 to Google Cloud Storage with Python (boto)
[python] Send the image captured from the webcam to the server and save it
[Python] Fluid simulation: From linear to non-linear
SIGNATE Quest ① From data reading to preprocessing
[Note] Get data from PostgreSQL with Python
From Python to using MeCab (and CaboCha)
HTML email with image to send with python
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)