Run Python CGI Application on Heroku - python

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.

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.

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.

To add to the top answer, for Python 3 the command in the Procfile should be this.
web: python -m http.server --cgi $PORT

Related

How to deploy a Python Dash Application on an internal company server?

I have written a Python Dash Application and it works completely fine on my local computer. Now, I want to be able to deploy this application on a server within the corporate network. I do NOT want to deploy this on Heroku etc because the datasource is an internal API. How do I go about deploying this application on the server? It's a Linux based machine.
I found this post that says use the code below but not quite sure where to add this piece of code.
waitress-serve --host=0.0.0.0 --port=8080 appname:app.server
The code you are referring to, waitress-serve, is a command-line wrapper bound to the function waitress.serve provided by Waitress.
You run it in your terminal or from a shell script.
Waitress is a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 3.7+.
You can install it with pip install waitress.
#see waitress-serve documentation here.

Setup for running simple python scripts on (Nginx) web server?

I would like to run a couple of very simple Python 3 scripts on the web. As an example, say, the script just reads certain parameters from the URL, sends an email and prints out a simple HTML page saying "success".
I have a virtual private server with Nginx, so I am free to setup the best framework.
My question is: what is the state-of-the-art setup to do this?
More particularly: what do I need to install on my server for Nginx and what do I use in my Python scripts for e.g. reading the URL content? My idea is, that once the server setup is done, I can just put any new script_xy.py file into some directory and it can be accessed using the URL of the script, without a full blown deployment for each script.
Flask If I were to use Flask (or Django), each new script would need its own, continuously running process and its own Nginx setup. That seems like a total overkill to me. Another alternative is web2py, is it the same here or would that be an idea?
CGI 20 years ago I used to program such simple things as Perl scripts using CGI. I read this can be done in principle with Python, but CGI is slow. Then there is Fast CGI. However, my impression was that this is still a bit outdated?
WSGI WSGI seems to be the state-of-the-art alternative to CGI for Python. What python modules would I need to import in my script and what would be the setup for Nginx?
Something else? As you see, I might just need a few hints on what to search for. I am not even sure if I need to search for "Python web framework" or "Python server" etc.
Any help is greatly appreciated.
Thanks a lot for your ideas!
juxeku

Run production Flask app locally, without a complex web server

I built a small web app for a friend. That friend's computer will not be connected to the Internet when using the app, so deploying it on Heroku is not an option.
Is there a way to deploy it locally without having to install a complex web server? Something small that can be packaged with the application? Using the built-in Flask server seems to be discouraged when you go to "production", but for a local app is it ok?
If it's just going to be used offline by one person, then yes, the internal development server might be sufficient.
If you're looking for a simple way to send that app to her, then see pyinstaller:
pip install pyinstaller
pyinstaller your_app.py
Zip up the folder inside the new dist directory and pass that along.
If pyinstaller isn't for you, there are plenty of options.
If you're just running the app locally, it should be fine. The main issues with the dev server are security and performance, but for an app that's not exposed to the outside and that has a single user, it should work fine. Even though you're using the dev server, it's still a good idea to turn off debug mode and enable multiprocess mode.
from multiprocessing import cpu_count
app.run(debug=False, processes=cpu_count())
If you want a little more performance, consider using uwsgi or gunicorn. Both are good WSGI app servers that can be installed with pip along with your application.
gunicorn -w $(nproc) --threads 2 --max-requests 10 myproject:app

Parallel Python install on Media Temple DV server

Plesk on the MediaTemple DV servers uses Python 2.4 for stuff, so the 2.4 install can't be replaced but someone recommended installing a separate python 2.7 install since my app runs on that. I'm new to the whole server thing, so this is new territory. My sense is that I can create a new directory for the source files and use SSH to download the files to said directory and then cd into it and install python 2.7. Then I have to figure out how to make sure Apache knows to use Python 2.7 to run the django app in question. Does this sound correct?
Unless you can make a system wide change to your python installation, you would have to run Apache, python and Django in the same virtual environment. If it is not possible, use gunicorn (instead of apache) to run the Django app in a virtualenv (in a port different from Apache's). If this app runs in a subdomain, you should consider hosting the Django app in a PAAS (Heroku, Google App Engine, etc.) which allow you to easily switch running environments.
Never mind, sorted it. the important thing seemed to be to edit the etc/ld.so.conf by adding usr/local/lib, then running sbin/ldconfig, which then makes ld.so.conf look like:
include ld.so.conf.d/*.conf
/usr/local/lib
Donwload and then compile Python
and use make && make altinstall.

Is it possible to run mod_python Python Server Pages outside of the Apache server context?

I have experience coding PHP and Python but have until recently not used Python for much web development.
I have started a project using mod_python with Python Server Pages which does a very good job of what I need it to do. Amongst the advantages of this is performance; the web server does not spawn a new interpreter for each request.
The system will finally be deployed on a server where I am able to setup /etc/apache/conf.d correctly, etc.
However, during development and for automated regression testing I would like the ability to run the .psp scripts without having to serve using an Apache instance. For example, it is possible to run PHP scripts using the PHP cli, and I would like to run my .psp scripts from the shell and see the resulting HTTP/HTML on stdout.
I suppose this mode would need to support simulation of POST requests, etc.
Update
OK, after typing all that question I discovered the mod_python command line in the manual:
http://modpython.org/live/current/doc-html/commandline.html
I guess that would get me most of the way there, which is to be able to exercise my application as a local user without deploying to an Apache server.
I am leaving this question here in case anyone does a web search like I did.
The mod_python command line tool is one way to do this.
See http://modpython.org/live/current/doc-html/commandline.html
Essentially,
mod_python create /path/to/new/server_root \
--listen 8888 \
--pythonpath=/path/to/my/app \
--pythonhandler=mod_python.wsgi \
--pythonoption="mod_python.wsgi.application myapp.wsgi::application"
sets up a skeleton app.
Then you run it:
mod_python start /path/to/new/server_root/conf/httpd.conf
wget http://localhost/path/to/my/app:8888
I am still testing this...
Caveat This function only seems to be from 3.4.0

Categories