Python API 无法使用 aiohttp 获取请求

Python API Unable to GET request using aiohttp

我正在尝试使用 aiohttp 创建自己的 api。它在 localhost:8080 上工作得很好,有没有办法将它连接到 heroku 站点,我尝试使用 https://dumboapi.herokuapp.com/getmeme/ 加载但它不起作用:/ 这是我的代码:

subreddit = ['memes', 'dankmemes', 'funny']

HEADERS = {
    'User-Agent' : "Dumbo"
}
async def getmeme():
    async with request("GET", f"https://www.reddit.com/r/{choice(subreddit)}/new.json?limit=100", headers=HEADERS) as resp:
        data = await resp.json()
        link_data = data['data']['children'][2]['data']['url_overridden_by_dest']
        title_data = data['data']['children'][2]['data']['title']
        score_data = data['data']['children'][2]['data']['score']
        submission = data['data']['children'][2]['data']['subreddit_name_prefixed']
        meme_data = {'image': f'{link_data}', 'title': f'{title_data}', 'score': f'{score_data}', 'subreddit': f'{submission}'}
        return meme_data

@routes.get('/getmeme')
async def handle(request):
    response = await getmeme()
    return web.Response(text=json.dumps(response))

async def initialize():
    app = web.Application()
    app.add_routes(routes)
    return app

web.run_app(initialize())

来自 Heroku 的错误:

2020-10-14T05:30:55.726041+00:00 heroku[worker.1]: Restarting
2020-10-14T05:30:55.739241+00:00 heroku[worker.1]: State changed from up to starting
2020-10-14T05:30:56.951514+00:00 heroku[worker.1]: Stopping all processes with SIGTERM
2020-10-14T05:30:57.134832+00:00 heroku[worker.1]: Process exited with status 0
2020-10-14T05:30:58.635865+00:00 heroku[worker.1]: Starting process with command `python main.py`
2020-10-14T05:30:59.240689+00:00 heroku[worker.1]: State changed from starting to up
2020-10-14T05:31:00.887027+00:00 app[worker.1]: ======== Running on http://0.0.0.0:8080 ========
2020-10-14T05:31:00.887045+00:00 app[worker.1]: (Press CTRL+C to quit)
2020-10-14T05:31:08.758433+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/getmeme/" host=dumboapi.herokuapp.com request_id=39a1af4e-c49b-45a5-b925-927ae0236996 fwd="IP" dyno= connect= service= status=503 bytes= protocol=https
2020-10-14T05:31:09.853957+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=dumboapi.herokuapp.com request_id=98f603ac-700f-495d-812a-23608c42fccd fwd="IP" dyno= connect= service= status=503 bytes= protocol=https

在 Heroku 上,您必须使用 Heroku 将在 PORT 环境变量中为您提供的 TCP 端口。 SSL 终止等将由 Heroku 路由层处理。

如果您(大致)将代码更改为:

,它应该可以工作
port=int(os.environ.get("PORT", "8080"))
web.run_app(initialize(), port=port)

Here 它表示 Only web dynos receive HTTP traffic from the routers.

据我所知,我想您没有使用网络进程类型。

2020-10-14T05:31:08.758433+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/getmeme/" host=dumboapi.herokuapp.com request_id=39a1af4e-c49b-45a5-b925-927ae0236996 fwd="IP" dyno= connect= service= status=503 bytes= protocol=https
2020-10-14T05:31:09.853957+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=dumboapi.herokuapp.com request_id=98f603ac-700f-495d-812a-23608c42fccd fwd="IP" dyno= connect= service= status=503 bytes= protocol=https

Procfile 中将进程类型更改为 web 同时从 heroku 提供的环境(PORT 变量)中读取端口号。