python-request gives cannot POST response

你离开我真会死。 提交于 2021-02-11 15:18:11

问题


I was trying to use python code with request to send the json data from Raspberry Pi computer to server through POST method

This is the request library and how to use it to POST json

import requests, Adafruit_DHT, time, json
from apscheduler.schedulers.background import BackgroundScheduler

sensor = Adafruit_DHT.AM2302 #DHT11/DHT22/AM2302
pin = 4
url = 'http://192.168.137.1:3350/temperature_humidity'
latest_humidity              = 0.0
latest_temperature           = 0.0
headers = {
    'content-type': 'application/json',
}
params = (
    ('priority', 'normal'),
)



def write_hist_value_callback():
  hum, temp = Adafruit_DHT.read_retry(sensor, pin)
  if hum is not None and temp is not None:
      if temp < -40.0 or temp > 80.0:
        print "Ignoring out of range temperature: {0:0.1f}*".format(temp)
      else:
        latest_temperature = temp
      if hum < 0.0 or hum > 100.0:
        print "Ignoring out of range humidity: {0:0.1f}%".format(hum)
      else:
        latest_humidity = hum
      payload = {'temperature': latest_temperature ,'humidity': latest_humidity}
      print json.dumps(payload)
      r = requests.post(url, headers=headers, params=params, json=payload)
      print(r.text)
print "Ignoring first 2 sensor values to improve quality..."
for x in range(2):
  Adafruit_DHT.read_retry(sensor, pin)

print "Creating interval timer. This step takes almost 2 minutes on the Raspberry Pi..."
#create timer that is called every n seconds, without accumulating delays as when using sleep
scheduler = BackgroundScheduler()
#scheduler.add_job(write_hist_value_callback, 'interval', seconds=60)
scheduler.start()
print "Started interval timer which will be called the first time in {0} seconds.".format(60);

try:
  while True:
    hum, temp = Adafruit_DHT.read_retry(sensor, pin)
    if hum is not None and temp is not None:
      if temp < -40.0 or temp > 80.0:
        print "Ignoring out of range temperature: {0:0.1f}*".format(temp)
      else:
        latest_temperature = temp #"{:.2f}".format(temp)
      if hum < 0.0 or hum > 100.0:
        print "Ignoring out of range humidity: {0:0.1f}%".format(hum)
      else:
        latest_humidity = hum #"{:.2f}".format(hum)
      payload = {'temperature': latest_temperature ,'humidity': latest_humidity}
      print json.dumps(payload)
      r = requests.post(url, headers=headers, params=params, json=payload)
      print(r.text)

    time.sleep(60)
except (KeyboardInterrupt, SystemExit):
  scheduler.shutdown()

On server side, I use NodeJS to set a server for it.

var express = require('express');
var port = 3350;
var app = express();
var bodyParser = require('body-parser');


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(__dirname + '/public'));
app.set('port', (process.env.PORT || 3350));

app.listen(app.get('port'), function () {
  console.log('run at port', app.get('port'));
})

Yet, when I ran the code on both side, it gave me the result

Ignoring first 2 sensor values to improve quality...
Creating interval timer. This step takes almost 2 minutes on the Raspberry Pi...
Started interval timer which will be called the first time in 60 seconds.
{"temperature": 24.8999996185, "humidity": 53.2999992371}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /temperature_humidity</pre>
</body>
</html>

Where did I go wrong and how should I do to make it POST to url properly?

来源:https://stackoverflow.com/questions/60474758/python-request-gives-cannot-post-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!