Run Python CGI Application on Heroku

2019-03-18 23:32发布

问题:

I made a simple application that makes use of Python CGI scripts. I have a working local version (works fine with lighttpd), but now I'd like to upload it to Heroku. The application consists of 2 or 3 scripts that make operations on a file and print information back to the browser, so I don't think I'll need any module other than CGI.

But the Heroku documentation only explains how to upload Python applications with fancy web frameworks, and I'm not using any of those.

I want to know if it's possible to run CGI scripts on Heroku, and if so, how to do it.

回答1:

Heroku Cedar is centered around self-hosting web applications, so you need to be able to bundle your application together and run it as a single command.

I think the easiest way would be to port your application to Flask. It isn't very complicated, especially if it is only 2 or 3 scripts.

Another option (depending on your performance requirements) would be to use the simple CGI server in the Python standard library and the Python buildpack. I think you would need to bundle up your scripts in a ./cgi-bin directory and start the server (in the procfile) with:

 web: bin/python -m CGIHTTPServer $PORT

The most complex way would be to bundle lighttpd and your scripts together and write a shell script to start it all up. You would have to make sure your compiled binaries are compatible with Heroku. I would look at the PHP buildpack as a starting point.



回答2:

I inquired with Heroku support about a cgi application that I tried to serve on Heroku's platform and here's the response:

Hello,

Unfortunately, we don't support CGI-style applications, only pure-Python ones. You may have some luck playing around with the Python CGIHTTPServer module some more, but if it doesn't suit your needs, you may be out of luck.



回答3:

To add to the top answer, for Python 3 the command in the Procfile should be this.

web: python -m http.server --cgi $PORT