从Python发送数据到节点服务器与套接字(NodeJS,Socket.io)

我试图从我的树莓pi3发送传感器数据(在python中)到我的本地节点服务器。

我发现了一个叫python的模块,用于向服务器发送数据。

在这里,我试图从我的树莓派3发送值22(以后会有传感器数据)到我的本地节点服务器与socket.io requests.get()的作品,但put命令不会发送数据。

你能告诉我错误在哪里吗?

#!/usr/bin/env python # import requests r = requests.get('http://XXX.XXX.XXX.XXX:8080'); print(r) r = requests.put('http://XXX.XXX.XXX.XXX:8080', data = {'rasp_param':'22'}); 

在我的server.js我尝试获取数据,但不知何故收到

server.js

 var express = require('express') , app = express() , server = require('http').createServer(app) , io = require('socket.io').listen(server) , conf = require('./config.json'); // Webserver server.listen(conf.port); app.configure(function(){ app.use(express.static(__dirname + '/public')); }); app.get('/', function (req, res) { res.sendfile(__dirname + '/public/index.html'); }); // Websocket io.sockets.on('connection', function (socket) { //Here I want get the data io.sockets.on('rasp_param', function (data){ console.log(data); }); }); }); // Server Details console.log('Ther server runs on http://127.0.0.1:' + conf.port + '/'); 

您正在使用Python的HTTP PUT,但您正在使用nodejs端的websocket服务器进行侦听。

要么有节点侦听HTTP POST(我会使用POST而不是PUT):

 app.post('/data', function (req, res) { //do stuff with the data here }); 

或者在Python的一边有一个websocket客户端:

 ws = yield from websockets.connect("ws://10.1.10.10") ws.send(json.dumps({'param':'value'})) 

持久的websocket连接可能是最好的select。