python - 如何将消息从纯数据发送到 Python?

标签 python sockets puredata

我正在尝试将消息从 Pure Data 发送到 Python(以显示在 SSD1306 OLED 上)。有人建议我使用套接字。他们还提供了以下 Python 代码:

import socket
s = socket.socket()
host = socket.gethostname()
port = 3000

s.connect((host, port))
mess = "hello"
Msg = mess + " ;"
s.send(message.encode('utf-8'))

在纯数据中,[netreceive 3000]对象和打印对象连接在一起。

这有效,但我想做完全相反的事情。使用套接字将数据从 Pure Data 发送到 Python。我找到了一些教程,但他们都谈到了 Python 到 Python 消息的接收和发送。我怎样才能实现 Pd 呢?

最佳答案

你可以使用 Python 的 socket库连接到通过 [netsend] 发送信息的 Pd 补丁.这是一个有效的最小示例。在 Pd 中,创建一个连接 [netsend] 的简单补丁。到您的 localhost 上的端口网络:

enter image description here

在 Python 中创建并保存监听器脚本(改编自 here)。下面的脚本使用 Python 3.6:

import socket
import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 13001)
print(f'starting up on {server_address[0]} port {server_address[1]}')
sock.bind(server_address)
sock.listen(1)

while True:
    print('waiting for a connection')
    connection, client_address = sock.accept()
    try:
        print('client connected:', client_address)
        while True:
            data = connection.recv(16)
            data = data.decode("utf-8")
            data = data.replace('\n', '').replace('\t','').replace('\r','').replace(';','')
            print(f'received {data}')
            if not data:
                break
    finally:
        connection.close()

运行这个脚本,然后运行 ​​Pd 的补丁。拖动该数字框会将值发送到 Python。在上面的这个例子中,所有接收到的值都只打印了它们的标签:

enter image description here

然后,您可以根据自己的需要调整这些。

关于python - 如何将消息从纯数据发送到 Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61285324/

相关文章:

c - 用于纯数据外部创建的 Eclipse 设置

python - 使用 Pyxll 将数组从 Excel 导入到 Python Pandas

python - 如何在 Python 中格式化 txt 文件

python - EWOULDBLOCK 套接字编程错误

ios - 将 Pd 补丁导出到 Apple App Store

parameters - 如何将创建参数传递给抽象?

Python,多处理 : what to do if process. join() 永远等待?

python - 如何更改名称/标签以删除 Django 管理中更改列表页面中的对象

c++ - 在 C++ 中下载一个 URL

perl - 在 Perl、套接字或命名管道 (fifos) 中什么更便携?