Running Python Script in Heroku

This post is to summarize some related information I found online when I was trying to put a python script into heroku to run. 

File Requirements

Expected files for Python

Heroku automatically identifies your app as a Python app if any of the following files are present in its root directory:

  • requirements.txt
  • setup.py
  • Pipfile

If none of these files is present in your app’s root directory, the Python buildpack will fail to identify your application correctly.

When you deploy to Heroku, the dependencies you specify in your requirements.txt file are automatically installed before app startup.
By default, your app continues to use whatever version of Python was used when it was first deployed. You can specify a different Python runtime to use in your app’s runtime.txt file.
Examples for those files:

You can check those files from Heroku CLI after you logged in:
$ cat requirements
cowpy==1.0.3
flask==1.0.2
sanic==19.6.0

Another example for  requirements.txt:

Flask==0.8
Jinja2==2.6
Werkzeug==0.8.3
certifi==0.0.8
chardet==1.0.1
distribute==0.6.24
gunicorn==0.14.2
requests==0.11.1

$ cat procfile
web: python index.py
worker: python index.py

$ cat runtime.txt
python-3.10.8

$ cat index.py
from http.server import BaseHTTPRequestHandler
class handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(self.headers.get('x-forwarded-for').encode())
return

Deploying a Github Project

Demo: https://myip.51sec.org

Github:  https://github.com/51sec/myip

Heroku CLI Commands


C:/Users/admin>heroku logs --tail -a pyweb-51sec
2022-10-22T16:48:03.936159+00:00 app[api]: Initial release by user [email protected]
2022-10-22T16:48:03.936159+00:00 app[api]: Release v1 created by user [email protected]
2022-10-22T16:48:04.194262+00:00 app[api]: Release v2 created by user [email protected]
2022-10-22T16:48:04.194262+00:00 app[api]: Enable Logplex by user [email protected]
2022-10-22T16:49:10.000000+00:00 app[api]: Build started by user [email protected]
2022-10-22T16:49:35.051261+00:00 app[api]: Deploy 84ee6f13 by user [email protected]
2022-10-22T16:49:35.051261+00:00 app[api]: Release v3 created by user [email protected]
2022-10-22T16:49:35.066863+00:00 app[api]: Scaled to web@1:Free by user [email protected]
2022-10-22T16:49:37.318734+00:00 heroku[web.1]: Starting process with command `python api/index.py`
2022-10-22T16:49:38.574743+00:00 app[web.1]: python: can't open file '/app/apiindex.py': [Errno 2] No such file or directory
2022-10-22T16:49:38.730339+00:00 heroku[web.1]: Process exited with status 2
2022-10-22T16:49:38.856084+00:00 heroku[web.1]: State changed from starting to crashed
2022-10-22T16:49:38.860038+00:00 heroku[web.1]: State changed from crashed to starting
2022-10-22T16:49:40.000000+00:00 app[api]: Build succeeded
2022-10-22T16:49:41.837516+00:00 heroku[web.1]: Starting process with command `python api/index.py`
2022-10-22T16:49:43.820729+00:00 app[web.1]: python: can't open file '/app/apiindex.py': [Errno 2] No such file or directory
2022-10-22T16:49:43.993065+00:00 heroku[web.1]: Process exited with status 2
2022-10-22T16:49:44.152351+00:00 heroku[web.1]: State changed from starting to crashed
2022-10-22T16:49:45.441959+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=pyweb-51sec.herokuapp.com request_id=607ce589-9fbe-4a0f-97b9-e1e470402ca6 fwd="209.141.173.61" dyno= connect= service= status=503 bytes= protocol=https
2022-10-22T16:49:46.065476+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=pyweb-51sec.herokuapp.com request_id=5b1670dd-7105-4942-86b0-eedd7f836819 fwd="209.141.173.61" dyno= connect= service= status=503 bytes= protocol=https
2022-10-22T16:55:27.866210+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=pyweb-51sec.herokuapp.com request_id=f2bcb8c3-5f6c-4266-a340-eb76a089d209 fwd="209.141.173.61" dyno= connect= service= status=503 bytes= protocol=https
2022-10-22T16:55:28.190676+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=pyweb-51sec.herokuapp.com request_id=2bf00c4d-fed1-41a0-b444-bca3e270aa67 fwd="209.141.173.61" dyno= connect= service= status=503 bytes= protocol=https
2022-10-22T16:55:33.000000+00:00 app[api]: Build started by user [email protected]
2022-10-22T16:55:49.183707+00:00 app[api]: Deploy 3ac70a84 by user [email protected]
2022-10-22T16:55:49.183707+00:00 app[api]: Release v4 created by user [email protected]
2022-10-22T16:55:49.889844+00:00 heroku[web.1]: State changed from crashed to starting
2022-10-22T16:55:53.000000+00:00 app[api]: Build succeeded
2022-10-22T16:55:55.153311+00:00 heroku[web.1]: Starting process with command `python index.py`
2022-10-22T16:55:56.552617+00:00 app[web.1]: python: can't open file '/app/index.py': [Errno 2] No such file or directory
2022-10-22T16:55:56.719727+00:00 heroku[web.1]: Process exited with status 2
2022-10-22T16:55:56.886874+00:00 heroku[web.1]: State changed from starting to crashed
^CTerminate batch job (Y/N)? y

C:/Users/admin>heroku run bash -a pyweb-51sec
Running bash on ⬢ pyweb-51sec... up, run.8205 (Free)
~ $ ls
api app Procfile README.md requirements.txt runtime.txt
~ $ exit
exit

C:/Users/admin>

Videos

 

References

版权声明:
作者:玉兰
链接:https://www.techfm.club/p/38117.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>