简体   繁体   English

将数据从python脚本发送到socket.io服务器

[英]Send data from python script to socket.io server

I have the following problem: 我有以下问题:

I hava a python script and a socket.io server running on my machine. 我的机器上运行着一个python脚本和一个socket.io服务器。 Now, I want to pass Data fom the python script to the Server and the client. 现在,我想将python脚本的数据传递给服务器和客户端。 At the moment, I am using the following: 目前,我正在使用以下内容:

Server: 服务器:

  fs.watchFile('position.txt', function (curr, prev) { //console.log('the current mtime is: ' + curr.mtime); //console.log('the previous mtime was: ' + prev.mtime); io.emit('positionswechsel', curr.mtime); //need the content of the file here }); 

Python: 蟒蛇:

def my_callback2(channel):
    fout=open("../sio/position.txt","w")
    fout.write("P1")
    fout.close()
print "falling edge detected on 18"

In this solution, I see a very high latency between the event (writing file) and the reaction of my server. 在此解决方案中,我看到事件(写入文件)与服务器的响应之间存在很高的延迟。

Now, I thought there should be a way to directly pass information from the python script to the server/client (I want to see the change on the webpage). 现在,我认为应该有一种方法可以将信息从python脚本直接传递到服务器/客户端(我想在网页上查看更改)。

Yan anybody please help me? 严有人请帮我吗?

Yannic 扬尼克

There is already a socket.io 1.0 client for Python . 已经有一个用于Pythonsocket.io 1.0客户端

If that doesn't work for you for whatever reason, you could always just establish a plain TCP connection (or use a UNIX socket) between the two processes and transfer newline-delimited JSON from Python to node, which then gets emitted to connection socket.io clients in some form or another. 如果由于某种原因这对您都不起作用,则始终可以在两个进程之间建立简单的TCP连接(或使用UNIX套接字),然后将换行符分隔的JSON从Python传输到节点,然后将其发送到连接套接字.io客户以某种形式出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM