针对 POST 请求,从 Node.js 服务器向 python 代码发送自定义响应
Send a custom response back to python code from the Node.js server, for a POST request
我是运行一个Python代码,用于post一些数据到我的Node.js服务器。在服务器接收到数据后,我需要 Node.js 服务器将自定义响应连同状态代码发送回 Python。
我正在使用纯 Node.js。没有使用 Express 或 Hapi 这样的框架。我的代码运行完美,但无法打印所需的消息。
我的部分Python代码用于post数据
import requests, json
payload = {
"DevId" : 'R',
"Sdata" : 'S',
"TimeS" : 'T',
"RSSI" : 'U'
}
jsonPayload=json.dumps(payload)
headers = {'Content-Type': 'application/json'}
post_res =requests.post(url='http://localhost:5555/',data=jsonPayload, headers=headers)
print post_response
我在 Node.js 服务器上尝试过的是。
1.
response.writeHead(200, { 'Content-Type': 'text/plain'});
response.end('Server has received the data')
输出 : <Response [400]>
2.
response.writeHead(200, { 'Content-Type': 'text/plain','Trailer': 'Server-Message' });
response.addTrailers({ 'Server-Message': 'Ok' });
response.end();
输出 : <Response [200]>
3.
var message = 'Invalid Device ID';
response.writeHead(400, message, {'content-type' : 'text/plain'});
response.end(message);
输出 : <Response [400]>
上面的代码我没有得到任何错误,所以我不知道我做错了什么。
我想要的输出是,连同我需要打印从服务器收到的自定义消息的状态代码,在终端中我正在执行 Python 代码。
<Response [200]> "The server has received the message"
打印 post_response.text
和 post_response
示例:
server.js:
const http = require('http')
const server = http.createServer((req,res) => {
res.setHeader('Content-Type', 'text/html');
res.writeHead(200, { 'Content-Type': 'text/plain'});
res.end('Server has received the message');
})
server.listen(3000, (err) => {
if(err) {
console.error('error');
}
console.log('server listening on port 3000');
})
response.py:
import requests
r = requests.post('http://localhost:3000', { 'Content-Type': 'application/json'});
print(str(r) + " " + r.text)
我是运行一个Python代码,用于post一些数据到我的Node.js服务器。在服务器接收到数据后,我需要 Node.js 服务器将自定义响应连同状态代码发送回 Python。
我正在使用纯 Node.js。没有使用 Express 或 Hapi 这样的框架。我的代码运行完美,但无法打印所需的消息。
我的部分Python代码用于post数据
import requests, json
payload = {
"DevId" : 'R',
"Sdata" : 'S',
"TimeS" : 'T',
"RSSI" : 'U'
}
jsonPayload=json.dumps(payload)
headers = {'Content-Type': 'application/json'}
post_res =requests.post(url='http://localhost:5555/',data=jsonPayload, headers=headers)
print post_response
我在 Node.js 服务器上尝试过的是。
1.
response.writeHead(200, { 'Content-Type': 'text/plain'});
response.end('Server has received the data')
输出 : <Response [400]>
2.
response.writeHead(200, { 'Content-Type': 'text/plain','Trailer': 'Server-Message' });
response.addTrailers({ 'Server-Message': 'Ok' });
response.end();
输出 : <Response [200]>
3.
var message = 'Invalid Device ID';
response.writeHead(400, message, {'content-type' : 'text/plain'});
response.end(message);
输出 : <Response [400]>
上面的代码我没有得到任何错误,所以我不知道我做错了什么。
我想要的输出是,连同我需要打印从服务器收到的自定义消息的状态代码,在终端中我正在执行 Python 代码。
<Response [200]> "The server has received the message"
post_response.text
和 post_response
示例:
server.js:
const http = require('http')
const server = http.createServer((req,res) => {
res.setHeader('Content-Type', 'text/html');
res.writeHead(200, { 'Content-Type': 'text/plain'});
res.end('Server has received the message');
})
server.listen(3000, (err) => {
if(err) {
console.error('error');
}
console.log('server listening on port 3000');
})
response.py:
import requests
r = requests.post('http://localhost:3000', { 'Content-Type': 'application/json'});
print(str(r) + " " + r.text)