首页 文章

无法在Heroku上运行一个非常简单的Python脚本

提问于
浏览
1

EDIT: A friend has told me the answer to this problem. This script is in fact working as intended, but Heroku is expecting to run this as web site while it's not a web service. That's where the problem is at!

Solution: Use a web framework, like Django. Ta-daa!


第一次使用Heroku ...脚本非常简单:获取一些JSON并显示它 .

import requests
import json

r = requests.get('http://steamcommunity.com/market/search/render/?query=&start=0&count=10&appid=570')
response = r.json()
print("Content-type: application/json")
print()
print(json.JSONEncoder().encode(response))

我有Procfile,我相信它是默认的(最基本的):

web: python server.py --port=$PORT

和requirements.txt文件:

requests==2.6.0

成功部署后,当我在浏览器中打开它时出现此错误:

应用程序错误应用程序中发生错误,无法提供您的页面 . 请稍后重试 . 如果您是应用程序所有者,请检查日志以获取详细信息 .

错误日志(简化了JSON结果,因为它很大):

2015-03-23T14:45:21.574258 00:00 heroku [web.1]:使用命令启动进程p ython test.py 2015-03-23T14:45:24.006938 00:00 app [web.1]:Content-类型:application / json 2015-03-23T14:45:24.007104 00:00 app [web.1]:{“pagesize”:10,“total_count”:196 15,“success”:true,“start”:0, “results_html”:“JSON正确显示”} 2015-03-23T14:45:24.006952 00:00 app [web.1] :()2015-03-23T14:45:24.697675 00:00 heroku [web.1]:进程退出状态0 2015-03-23T14:45:24.716774 00:00 heroku [web.1]:状态从开始变为c rahedhed 2015-03-23T14:45:47.384763 00:00 heroku [router]:at =错误代码= H10 desc =“App cra shed”方法= GET path =“/”host = dry-forest-3041.herokuapp.com request_id = 402eaf01 -c1c0-4ce7-9164-20b2c8446cda fwd =“76.65.214.143”dy no = connect = service = status = 503 bytes = 2015-03-23T14:45:48.711722 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/ favicon.ico “host = dry-forest-3041.herokuapp.com request_ id = 533995dd-50a2-403a-ba31-8c436da8c825 fw d =“76.65.214.143”dyno = connect = servi ce = status = 503 bytes = 2015-03-23T14:45:49.222004 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”方法= GET path =“/”host = dry-forest-3041.herokuapp.com request_id = c2ee68af -f295-4ca9-b035-3a97e88f105b fwd =“76.65.214.143”dyno = connect = service = status = 503 bytes = 2015-03 -23T14:45:49.502723 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/ favicon.ico”host = dry-forest-3041.herokuapp.com request_ id = ef98b883-0995-4713-b8b5-cdee908ba868 fwd =“76.65.214.143”dyno = connect = servi ce = status = 503 bytes = 2015-03-23T14:45:50.164614 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/”host = dry-forest-3041.herokuapp.com request_id = aabced87 -6c3b-4945-b97b-a7010a0f58dd fwd =“76.65.214.143”dyno = connect = service = status = 503 bytes = 2015-03-23T14:45:50.480625 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/ favicon.ico”host = dry-forest-3041.herokuapp.com request_ id = 6 c950374-d9cd-4c4a-8951-e51410cf4d7c fwd =“76.65.214.143”dyno = connect = servi ce = status = 503 bytes = 2015-03-23T14:45:50.910714 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”方法= GET path =“/”host = dry-forest-3041.herokuapp.com request_id = c247d63e -bb9b-4a22-8906-9c5a5a44dda9 fwd =“76.65.214.143”dyno = connect = service = status = 503 bytes = 2015-03-23T14:45:51.221878 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/ favicon.ico”host = dry -forest-3041.herokuapp.com request_id = 30b11aab-e7b9-493a-850f-e6fe5534a818 fwd =“76.65.214.143”dyno = connect = servi ce = status = 503 bytes = 2015-03-23T14:45:51.674624 00: 00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/”host = dry-forest-3041.herokuapp.com request_id = a6584cc3 -e633-4f79-82b8-42077a242069 fwd =“76.65.214.143”dyno = connect = service = status = 503 bytes = 2015-03-23T14:45:51.960557 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/ favicon.ico”host = dry-f orest-3041.herokuapp.com request_ id = 80236a6d-9283-49dc-ac2d-e1814152dbe8 fwd =“76.65.214.143”dyno = connect = servi ce = status = 503 bytes = 2015-03-23T14:48:45.637494 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/”host = dry-forest-3041.herokuapp.com request_id = a3b95988 -65ee-4a89-a477-4b9259dca26f fwd = “76.65.214.143”dyno = connect = service = status = 503 bytes = 2015-03-23T14:48:45.948123 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/ favicon.ico”host = dry-forest-3041.herokuapp.com request_id = d95ea486-3615-4402-9725-fbfdc3eec9ab fwd =“76.65.214.143”dyno = connect = servi ce = status = 503 bytes = 2015 -03-23T14:48:46.451410 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/”host = dry-forest-3041.herokuapp.com request_id = b553aa00 -1386-479c-9bf3-06c014a92074 fwd =“76.65.214.143”dyno = connect = service = status = 503 bytes = 2015-03-23T14:48:46.707337 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”遇见了hod = GET path =“/ favicon.ico”host = dry-forest-3041.herokuapp.com request_ id = 846c269b-4b92-4146-8c21-78a0ec9e8ee6 fwd =“76.65.214.143”dyno = connect = servi ce = status = 503字节= 2015-03-23T14:48:47.269222 00:00 heroku [路由器]:at =错误代码= H10 desc =“App cra shed”方法= GET path =“/”host = dry-forest-3041.herokuapp .com request_id = 82ece324-fe04-42cb-835e-3a9214c36c4a fwd =“76.65.214.143”dyno = connect = service = status = 503 bytes = 2015-03-23T14:48:47.578761 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/ favicon.ico”host = dry-forest-3041.herokuapp.com request_ id = 7d76b309-bbae-4dc1-97eb-d8312cce9a12 fwd =“76.65.214.143”dyno = connect = servi ce = status = 503 bytes = 2015-03-23T14:57:57.751643 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/”host = dry -forest-3041.herokuapp.com request_id = 1eb273ec -ffd7-42ba-9c95-e92e6957449f fwd =“76.65.214.143”dyno = connect = service = status = 503 bytes = 2015-03-23T14:57:58.032275 00:00 heroku [router]:at = error code = H10 desc =“App cra shed”method = GET path =“/ favicon.ico”host = dry-forest-3041.herokuapp.com request_ id = 0061f766-4c8d-4b49-9f0e- d9174e16f5fa fwd =“76.65.214.143”dyno = connect = servi ce = status = 503 bytes =

3 回答

  • 0

    我曾经使用过heroku但只是在部署Ruby on Rails应用程序时所以我在黑暗中刺伤但它可能与请求模块和heroku无法导入它有关 .

    你设置了这样的版本号(最新2.6.0)吗?

    requests==2.6.0
    
  • 0

    尝试使用:

    pip freeze > requirements.txt
    

    对于需求文件 .

  • 0

    我在Heroku上运行了太多脚本,你必须包含2个文件

    **requirements.txt**
    requests==2.5.3
    gunicorn==0.17.2
    wsgiref==0.1.2
    
    **Procfile** 
    web: gunicorn script_file_name:app --log-file=-
    

    如果您需要更多帮助,欢迎您

相关问题