Heroku-未找到 (Python)

2024-04-07

作为一个初学者,我无法再进一步。 我正在尝试在heroku上加载python程序,但不知何故总是只出现以下内容:

未找到

在服务器上找不到请求的 URL。如果您手动输入 URL,请检查拼写并重试。

My logs:

2018-02-12T11:33:54.658010+00:00 heroku[web.1]: Starting process with 
command `python app.py`
2018-02-12T11:33:56.853591+00:00 heroku[web.1]: State changed from starting 
to up
2018-02-12T11:33:56.619420+00:00 app[web.1]: Starting app on port 50619
2018-02-12T11:33:56.661819+00:00 app[web.1]:  * Running on 
http://0.0.0.0:50619/ (Press CTRL+C to quit)
2018-02-12T11:34:48.437183+00:00 app[web.1]: 10.5.180.220 - - [12/Feb/2018 
11:34:48] "GET / HTTP/1.1" 404 -
2018-02-12T11:34:48.445571+00:00 heroku[router]: at=info method=GET 
path="/" host=protected-citadel-58416.herokuapp.com request_id=e8d60622-
5acd-4ddf-9b98-87307b1266b7 fwd="79.213.120.187" dyno=web.1 connect=1ms 
service=29ms status=404 bytes=380 protocol=https
2018-02-12T11:40:15.749446+00:00 heroku[router]: at=info method=GET path="/" 
host=protected-citadel-58416.herokuapp.com request_id=8e5c9f8e-c750-48bd-
a8ae-d40e7d3ea1d6 fwd="79.213.120.187" dyno=web.1 connect=0ms service=3ms 
status=404 bytes=380 protocol=https
2018-02-12T11:40:15.743311+00:00 app[web.1]: 10.79.228.240 - - [12/Feb/2018 
11:40:15] "GET / HTTP/1.1" 404 -
2018-02-12T11:40:57.718508+00:00 heroku[router]: at=info method=GET path="/" 
host=protected-citadel-58416.herokuapp.com request_id=312900cd-79d6-4f9c-
9c5b-a40d356219bb fwd="79.213.120.187" dyno=web.1 connect=0ms service=3ms 
status=404 bytes=380 protocol=https
2018-02-12T11:40:57.712039+00:00 app[web.1]: 10.79.228.240 - - [12/Feb/2018 
11:40:57] "GET / HTTP/1.1" 404 -

我的程序(对话流网络钩子,也可能是问题):

from __future__ import print_function
from future.standard_library import install_aliases
install_aliases()

from urllib.parse import urlparse, urlencode, quote_plus
from urllib.request import urlopen, Request
from urllib.error import HTTPError

import json
import os

from flask import Flask
from flask import request
from flask import make_response

# Flask app should start in global layout
app = Flask(__name__)


@app.route('/webhook', methods=['POST'])
def webhook():
    req = request.get_json(silent=True, force=True)

    print("Request:")
    print(json.dumps(req, indent=4))

    res = processRequest(req)

    res = json.dumps(res, indent=4)
    # print(res)
    r = make_response(res)
    r.headers['Content-Type'] = 'application/json'
    return r


def processRequest(req):
    print("Request:")
    print(json.dumps(req, indent=4))
    if req.get("result").get("action") == "getpassword":
         baseurl = "http://www.passwordrandom.com/query?command=password&format=json&count=1"
         if yql_query is None:
             return {}
         yql_url = baseurl 
         result = urlopen(yql_url).read()
         data = json.loads(result)
         res = makeWebhookResult(data)
    else:
        return {}
    return res

def makeWebhookResult(data):
    query = data.get('queryresult')
    if queryresult is None:
        return {}

    password = query.get('char')


    # print(json.dumps(item, indent=4))

    speech = "Your new password is " + password

    print("Response:")
    print(speech)

    return {
        "speech": speech,
        "displayText": speech,
        "data": {...},
        "contextOut": [...],
        "source": "WolframAlpha"
    }


if __name__ == '__main__':
    port = int(os.getenv('PORT', 5000))

    print("Starting app on port %d" % port)

app.run(debug=False, port=port, host='0.0.0.0')

我的程序文件:

web: python app.py

我的要求.txt:

certifi==2018.1.18
chardet==3.0.4
click==6.7
flake8==3.5.0
Flask==0.12.2
future==0.16.0
idna==2.6
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
mccabe==0.6.1
pew==1.1.2
pipenv==9.0.3
psutil==5.3.1
pycodestyle==2.3.1
pyflakes==1.6.0
requests==2.18.4
urllib3==1.22
virtualenv==15.1.0
virtualenv-clone==0.2.6
Werkzeug==0.14.1

我究竟做错了什么? 预先非常感谢

这是我的模板 https://github.com/dialogflow/fulfillment-webhook-weather-python/blob/master/app.py

UPDATE

代码已按照建议进行了更改。

Log:

2018-02-12T15:12:39.069747+00:00 app[web.1]: Request:
2018-02-12T15:12:39.069877+00:00 app[web.1]: {
2018-02-12T15:12:39.069880+00:00 app[web.1]:     "id": "14fe0cb2-ce54-48cd-9569-87fc55b6cacc",
2018-02-12T15:12:39.069882+00:00 app[web.1]:     "timestamp": "2018-02-12T15:12:38.929Z",
2018-02-12T15:12:39.069884+00:00 app[web.1]:     "lang": "en",
2018-02-12T15:12:39.069885+00:00 app[web.1]:     "result": {
2018-02-12T15:12:39.069886+00:00 app[web.1]:         "source": "agent",
2018-02-12T15:12:39.069887+00:00 app[web.1]:         "resolvedQuery": "make a password",
2018-02-12T15:12:39.069889+00:00 app[web.1]:         "speech": "",
2018-02-12T15:12:39.069890+00:00 app[web.1]:         "action": "getpassword",
2018-02-12T15:12:39.069891+00:00 app[web.1]:         "actionIncomplete": false,
2018-02-12T15:12:39.069892+00:00 app[web.1]:         "parameters": {
2018-02-12T15:12:39.069894+00:00 app[web.1]:             "passwordlenght": ""
2018-02-12T15:12:39.069895+00:00 app[web.1]:         },
2018-02-12T15:12:39.069896+00:00 app[web.1]:         "contexts": [],
2018-02-12T15:12:39.069897+00:00 app[web.1]:         "metadata": {
2018-02-12T15:12:39.069898+00:00 app[web.1]:             "intentId": "f46eb1ca-e0e3-428f-8442-8e6327b935d7",
2018-02-12T15:12:39.069900+00:00 app[web.1]:             "webhookUsed": "true",
2018-02-12T15:12:39.069901+00:00 app[web.1]:             "webhookForSlotFillingUsed": "false",
2018-02-12T15:12:39.069902+00:00 app[web.1]:             "intentName": "GeneratePassword"
2018-02-12T15:12:39.069903+00:00 app[web.1]:         },
2018-02-12T15:12:39.069904+00:00 app[web.1]:         "fulfillment": {
2018-02-12T15:12:39.069906+00:00 app[web.1]:             "speech": "The service is currently not aviable.",
2018-02-12T15:12:39.069907+00:00 app[web.1]:             "messages": [
2018-02-12T15:12:39.069908+00:00 app[web.1]:                 {
2018-02-12T15:12:39.069909+00:00 app[web.1]:                     "type": 0,
2018-02-12T15:12:39.069911+00:00 app[web.1]:                     "speech": "The service is currently not aviable."
2018-02-12T15:12:39.069913+00:00 app[web.1]:                 }
2018-02-12T15:12:39.069915+00:00 app[web.1]:             ]
2018-02-12T15:12:39.069916+00:00 app[web.1]:         },
2018-02-12T15:12:39.069918+00:00 app[web.1]:         "score": 1.0
2018-02-12T15:12:39.069919+00:00 app[web.1]:     },
2018-02-12T15:12:39.069921+00:00 app[web.1]:     "status": {
2018-02-12T15:12:39.069922+00:00 app[web.1]:         "code": 200,
2018-02-12T15:12:39.069924+00:00 app[web.1]:         "errorType": "success",
2018-02-12T15:12:39.069925+00:00 app[web.1]:         "webhookTimedOut": false
2018-02-12T15:12:39.069927+00:00 app[web.1]:     },
2018-02-12T15:12:39.069929+00:00 app[web.1]:     "sessionId": "dbb472ca-cd7d-4c03-b666-66d90b458f22"
2018-02-12T15:12:39.069934+00:00 app[web.1]: }
2018-02-12T15:12:39.341869+00:00 app[web.1]: 10.45.77.75 - - [12/Feb/2018 15:12:39] "POST / HTTP/1.1" 200 -
2018-02-12T15:12:39.344514+00:00 heroku[router]: at=info method=POST path="/" host=protected-citadel-58416.herokuapp.com request_id=842f0891-6768-48e9-a6d8-740b8dc4a8d6 fwd="35.202.154.33" dyno=web.1 connect=2ms service=275ms status=200 bytes=147 protocol=https

对话流中的 JSON:

{
  "id": "947ae098-542c-4208-b49a-eed69fb001fd",
  "timestamp": "2018-02-12T15:30:24.471Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "make a password",
    "action": "getpassword",
    "actionIncomplete": false,
    "parameters": {
      "passwordlenght": ""
    },
    "contexts": [],
    "metadata": {
      "intentId": "f46eb1ca-e0e3-428f-8442-8e6327b935d7",
      "webhookUsed": "true",
      "webhookForSlotFillingUsed": "false",
      "webhookResponseTime": 290,
      "intentName": "GeneratePassword"
    },
    "fulfillment": {
      "messages": [
        {
          "type": 0,
          "speech": "The service is currently not aviable."
        }
      ]
    },
    "score": 1
  },
  "status": {
    "code": 200,
    "errorType": "success",
    "webhookTimedOut": false
  },
  "sessionId": "dbb472ca-cd7d-4c03-b666-66d90b458f22"
}

您似乎没有设置以下路线/请求,只是为了 POST 到/webhook小路。看起来您正在尝试通过在浏览器中输入地址来手动测试它 - 这对您没有多大帮助。

同样,您似乎尚未在 Dialogflow 中配置 webhook 来使用/webhook小路。在履行部分,您应该设置完整 URL 的 URL - 包括路径。它可能看起来像这样(注意它如何以 /webhook 结尾):

https://YourHostNameHere:50619/webhook

您应该能够使用诸如curl 之类的工具通过POST 来访问URL。您需要确保它是通过 HTTPS 并且发送带有有效标头的 JSON。可能是这样的

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST https://YourHostNameHere:50619/webhook

或者,如果文件中有 JSON

curl -d "@file.json" -X POST https://YourHostNameHere:50619/webhook

您还可以尝试通过 Dialogflow 的测试侧边栏 - 如果模式与应通过 Webhook 实现的内容匹配,它会将其发送到那里。

但这可能仍然无法按照您的预期工作。它看起来不像从返回makeWebhookResult()是有效的 JSON,这会混淆代码和 Dialogflow 中的各种内容。

Update稍微解释一下你的代码

您的代码中的这些行:

@app.route('/webhook', methods=['POST'])
def webhook():

非常粗略地说“我正在定义一个名为webhook()。当您在该服务的 URL 上收到以字符串结尾的 POST 请求时,您应该调用此函数/webhook."

因此,如果您在以下位置举办此活动:heroku.example.com在端口 59123 上使用 HTTPS,则 Webhook 的完整 URL 将是https://heroku.example.com:59123/webhook,这是您需要将其输入 Dialogflow 实现页面的 URL。

您的日志表明您正在调用此函数而无需/webhook部分。特别是这条线

2018-02-12T14:23:15.577096+00:00 heroku[router]: at=info method=POST path="/" host=protected-citadel-58416.herokuapp.com request_id=b1b7e6d8-fd75-44e5-a2ab-1a73ea303f80 fwd="35.192.209.96" dyno=web.1 connect=0ms service=57ms status=404 bytes=380 protocol=https

建议,虽然它是在您的主机上使用 HTTPS 和 POST 调用的,但它尝试路由到的路径是“/”而不是“/webhook”。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Heroku-未找到 (Python) 的相关文章

随机推荐