【问题标题】:Heroku-Not Found (Python)Heroku-未找到 (Python)
【发布时间】:2018-02-14 00:56:46
【问题描述】:

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

未找到

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

我的日志:

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 -

我的程序(一个 dialogflow webhook,也可能是问题所在):

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

我做错了什么? 提前非常感谢您

This is my template

更新

已按照建议更改了代码。

日志:

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"
}

【问题讨论】:

  • 如果您希望有人帮助您解决问题,您需要提供更多关于您所做工作的信息。复制/粘贴日志是一个好的开始,但显然还不够。

标签: python heroku actions-on-google dialogflow-es


【解决方案1】:

您似乎没有为/ 请求设置路由,只是为/webhook 路径的POST。您似乎正在尝试通过在浏览器中输入地址来手动测试它 - 这对您没有太大帮助。

同样,您似乎没有在 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 中的各种内容。

更新来稍微解释一下你的代码

您的代码中的这些行:

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

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

因此,如果您使用 HTTPS 在端口 59123 上的 heroku.example.com 上托管此内容,则 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”。

【讨论】:

  • 谢谢!我更新了我的代码并包含了一个链接(我的模板)。我试图用测试侧边栏调用它,但它不起作用。我不太明白您的第一个改进建议。
  • 更新了我的答案 - 由于同样的原因,您的履行 URL 似乎不正确 - 该 URL 应包含完整路线。在这种情况下,包括“/webhook”。
  • 再次更新,这次对您的代码要求的内容和日志显示的内容进行了更长的解释。 (另一个更新是答案中的第二段。)
  • 我觉得我们快到了!如果我通过对话流发送请求,至少不会再有错误!不幸的是,只会触发标准答案,而我尝试发送的答案将被完全忽略或不会到达。我需要改变什么?我添加了 JSON 文件。
  • 我会重构你的代码,看看req.get("result").get("action") 的值是多少。您报告的内容听起来像 return {} 是被触发的内容。我会把你的调试重点放在那里。
猜你喜欢
  • 2013-03-18
  • 1970-01-01
  • 2021-09-09
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多