烧瓶-找不到

时间:2018-07-15 13:07:13

标签: python flask

我已经构建了一个小烧瓶应用程序,该应用程序侦听HTTP POST请求并将数据发送到MQTT。但是我无法在本地网络上运行它。

#!/usr/local/bin/python


from flask import Flask, request
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import ssl


app = Flask(__name__)

@app.run(host = '0.0.0.0', port=5000)

@app.route('/mqtt', methods=['GET'])

def get_id():

  topic = request.args.get('topic')
  temp = request.args.get('temp')
  hum = request.args.get('hum')
  pre = request.args.get('pre')
  auth = {
         'username':"user",
         'password':"pw
  tls = {
         'ca_certs':"/etc/ssl/certs/ca-certificates.crt",
         'tls_version':ssl.PROTOCOL_TLSv1
       }
  publish.single(topic,
  payload= temp + " " +  hum + " " + pre,
  hostname="domain",
  client_id="REST_API",
  auth=auth,
  tls=tls,
  port=8883,
  protocol=mqtt.MQTTv311)

  return("OK")

if __name__ == '__main__':
  app.run(debug=True)

查询如下:

curl --noproxy -x POST "192.168.2.56:5000/mqtt?topic=simcom7000e1&temp=25&hum=10&pre=99"

错误:

curl: (6) Could not resolve host: POST
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>

如果我将其注释掉:

@app.run(host = '0.0.0.0', port=5000)

然后运行:

curl --noproxy -x POST "localhost:5000/mqtt?topic=simcom7000e1&temp=25&hum=10&pre=99"

然后它起作用。对我来说,这真的很奇怪。可能是什么问题?

2 个答案:

答案 0 :(得分:0)

您使用GET进行了路由,但尝试使用POST进行了请求。并且您可以尝试通过以下方式注册:

@app.route('/mqtt', methods=['GET', 'POST'])

答案 1 :(得分:0)

解决方案是:

#!/usr/local/bin/python


from flask import Flask, request
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import ssl


app = Flask(__name__)


@app.route('/mqtt', methods=['GET'])

def get_id():

  topic = request.args.get('topic')
  temp = request.args.get('temp')
  hum = request.args.get('hum')
  pre = request.args.get('pre')
  auth = {
         'username':"user",
         'password':"pw
  tls = {
         'ca_certs':"/etc/ssl/certs/ca-certificates.crt",
         'tls_version':ssl.PROTOCOL_TLSv1
       }
  publish.single(topic,
  payload= temp + " " +  hum + " " + pre,
  hostname="domain",
  client_id="REST_API",
  auth=auth,
  tls=tls,
  port=8883,
  protocol=mqtt.MQTTv311)

  return("OK")

if __name__ == '__main__':
  app.run(debug=True), host = '0.0.0.0', port=5000