如何将 Heroku 端口绑定到 scrapyd

How to bind Heroku port to scrapyd

我在 Heroku 上创建了一个简单的 python 应用程序来启动 scrapyd。 scrapyd 服务启动,但它在端口 6800 上启动。Heroku 要求您将它绑定到 $PORT 变量,我能够在本地 运行 heroku 应用程序。该过程的日志包含在下面。我查看了这个包的一个包 scrapy-heroku, but wasn't able to install it due to errors. The code in app.py 似乎提供了一些关于如何完成的线索。我如何将其实现为 python 命令以在 Heroku 提供的端口上启动 scrapyd?

过程文件:

web: scrapyd

Heroku 日志:

2022-01-24T05:17:27.058721+00:00 app[web.1]: 2022-01-24T05:17:27+0000 [twisted.scripts._twistd_unix.UnixAppLogger#info] twistd 21.7.0 (/app/.heroku/python/bin/python 3.10.2) starting up.
2022-01-24T05:17:27.058786+00:00 app[web.1]: 2022-01-24T05:17:27+0000 [twisted.scripts._twistd_unix.UnixAppLogger#info] reactor class: twisted.internet.epollreactor.EPollReactor.
2022-01-24T05:17:27.059190+00:00 app[web.1]: 2022-01-24T05:17:27+0000 [-] Site starting on 6800
2022-01-24T05:17:27.059301+00:00 app[web.1]: 2022-01-24T05:17:27+0000 [twisted.web.server.Site#info] Starting factory <twisted.web.server.Site object at 0x7f1706e3eaa0>
2022-01-24T05:17:27.059649+00:00 app[web.1]: 2022-01-24T05:17:27+0000 [Launcher] Scrapyd 1.3.0 started: max_proc=32, runner='scrapyd.runner'
2022-01-24T05:18:25.204305+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2022-01-24T05:18:25.231596+00:00 heroku[web.1]: Stopping process with SIGKILL
2022-01-24T05:18:25.402503+00:00 heroku[web.1]: Process exited with status 137

您只需要读取 PORT 环境变量并将其写入您的 scrapyd 配置文件即可。您可以查看执行相同操作的代码。

# init.py
import os
import io

PORT = os.environ['PORT']
with io.open("scrapyd.conf", 'r+', encoding='utf-8') as f:
    f.read()
    f.write(u'\nhttp_port = %s\n' % PORT)

来源:https://github.com/scrapy/scrapyd/issues/367#issuecomment-591446036