본문 바로가기

Programming Language/Python

Socket을 이용한 Python과 Processing 간의 통신

Processing client.pde

import processing.net.*; 
Client myClient;

void setup() { 
  size(200, 200); 
  myClient = new Client(this, "127.0.0.1", 3030); 
} 

void draw() { 
  myClient.write("Hello"); // send whatever you need to send here
  String kk = myClient.readString();
  println(kk);
} 

 

Python server.py

import socket

HOST = '127.0.0.1'
PORT = 3030
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
    data = conn.recv(1024).decode()
    if not data: break
    kk = str(data) + ' world!'
    conn.send(kk.encode())

conn.close()

 

결과

 

 

Processing과 Python이 서로 데이터를 주고 받는데 성공했다! :-)