11

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.

user1002327
  • 533
  • 2
  • 8
  • 23
  • Deploy it how you would static files, except into a CGI-enabled directory. – Ignacio Vazquez-Abrams Nov 22 '12 at 22:52
  • I'm completely new to Heroku. I'm not sure how to get a CGI-enabled directory. They also mention something about virtualenv, not sure if I'd have to do that to upload the CGI files. If I understood correctly, I simply have to clone the git repo, copy the files to the root directory, commit, push and done? – user1002327 Nov 22 '12 at 22:55
  • virtualenv has nothing (okay, very little) to do with it, and enabling CGI is a web server issue, not a Heroku issue per se. – Ignacio Vazquez-Abrams Nov 22 '12 at 22:56

3 Answers3

8

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.

groodt
  • 1,955
  • 15
  • 26
  • I didn't know Python had a web server with CGI support. That's pretty cool. I'll try the second or the third way, since I have little to none interest in porting the application. – user1002327 Nov 22 '12 at 23:07
  • 1
    I hope it works out for you. I would try the CGI server locally first, to see if it is capable of running your scripts and also to work out the directory structure. Then figure out how to package it up for Heroku. – groodt Nov 22 '12 at 23:17
  • The scripts don't use any weird Python construct, I guess they will work fine. I'll try the server locally and accept your answer when I upload everything. Thanks. – user1002327 Nov 22 '12 at 23:45
  • At cuberoot, omit the `bin/` folder and your second option works. That is just call `web: python -m CGIHTTPServer $PORT`. I was getting an Application Error and changing that fixed it. Thanks for the great answer! – Jet Blue Nov 19 '15 at 04:01
0

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.

mfab
  • 21
  • 1
0

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

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