How to let the webserver (e.g. Apache) call Python directly?

how to run python script on apache server linux
how to run python program in web browser
python http server
execute python script from html page
python in html example
python apache web server
apache python
python web application example

(Important) Disclaimer: I know it's probably not a good idea, that Python is not like PHP, and that the "natural" way to do web with Python is more by using a framework like Bottle, Flask, Django (that I already use), etc. But still, just out of curiosity, I'd like to see how the following is possible.


When Apache + PHP are installed, we can access a page like http://www.example.com/index.php. Internally, Apache probably passes the request to PHP which executes code, produces a text output, which is then served by Apache.

Question: how could we do something similar in Python? i.e. by accessing http://www.example.com/index.py, Apache would call the script index.py:

print("<html><body>Hello world</body></html>")

and then Apache would serve this page to the client.


NB:

  • Calling http://www.example.com/index.py?foo=bar could even give the params to the Python script in sys.argv

  • I already did it like this: http://www.example.com/index.php:

    <?php 
    $out = shell_exec("python index.py"); 
    echo($out); 
    ?>
    

    which then calls the Python script and produces the output. It works, but I'd like to do it without PHP.

  • Said in another way, is there something like mod_php for Python?


There exists a similar mod for python, but it's not as widely used and does not seem to have been updated for a few years.

Note: A common way of doing things is using apache/nginx as a web server, and uwsgi as an application server, with the web server redirecting to the application server for non-static content urls.

Apache HTTP Server, How do I run a Python program on a Web server? Now, the apache web server needs to be restarted. You can do this either via the Apache service located in the services control panel or via the Start -> All Programs -> Apache . . . -> Control Apache Server menu option. 4. Run a test Python page. You can use the EditRocket hello world Python sample web page template for a test Python page.


How to incorporate Python into HTML, Most HTTP servers are written in C or C++, so they cannot execute Python code most web servers will let the user download it, instead of running it and it is not possible to switch from an older version to a newer (e.g. 2.4 to 2.5) started to directly embed WSGI applications into the Apache web server. There are many web server software that support HTTP protocol such as apache, nginx, and lighttpd. In this moment, I’d like to show you how to make something like that (a simple version obviously) in python language.


There is one more module apache can able to server python apart from mod_python which is mod_wsgi you may have tried it already if not this can be done like below.

First install if not already installed

sudo apt-get install libapache2-mod-wsgi -y

Create vhost

<VirtualHost *:8081>

    #ServerName www.example.com
    #ServerAlias example.com
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/html/wsgi

    <Directory /var/www/html/wsgi>
    <IfVersion < 2.4>
        Order allow,deny
        Allow from all
    </IfVersion>
    <IfVersion >= 2.4>
        Require all granted
    </IfVersion>
    </Directory>

    WSGIScriptAlias /myapp /var/www/html/wsgi/index.py #path to file

    <Directory /var/www/html/wsgi>
    <IfVersion < 2.4>
        Order allow,deny
        Allow from all
    </IfVersion>
    <IfVersion >= 2.4>
        Require all granted
    </IfVersion>
    </Directory>

</VirtualHost>

Create index.py

def application(environ, start_response):
    status = '200 OK'
    output = b'<h2>Hello World!</h2>'

    response_headers = [('Content-type', 'text/html'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

HOWTO Use Python in the web, Configuring the Apache Web Server to Run Python on Windows. Install Python. You can get Python from the following: http://python.org/download/. Configure Apache to run Python CGI. The next step is to use EditRocket to open the httpd.conf apache configuration file located in the apache install directory in the conf Most HTTP servers are written in C or C++, so they cannot execute Python code directly – a bridge is needed between the server and the program. These bridges, or rather interfaces, define how programs interact with the server. There have been numerous attempts to create the best possible interface, but there are only a few worth mentioning.


Configuring the Apache Web Server to Run Python on Windows, The first step is getting Apache2 to recognize that my .py files were to be sometimes I just want to get my webserver up and running and start  I’ve been developing for years (C, C++, PHP), but had never delved into python before, and I wanted to be able to have my scripts have a web interface. The first step is getting Apache2 to recognize that my .py files were to be executed and not spit out as text files.


Configuring Apache2 to run Python Scripts, In this article, we'll use the SimpleHTTPServer module to quickly run a Python app and access Widely used web servers such as Apache, Monkey, and Jigsaw are quite It comes packaged with a simple HTTP server that delivers standard GET and HEAD This is the simplest way to directly serve files locally over HTTP. Step 4 — Installing Apache 2. In this section, we will install Apache 2, and ensure that it recognizes Python files as executables. Install Apache using apt-get: sudo apt-get install apache2 Like MySQL, the Apache server will start once the installation completes. Note: After installation, several ports are open to the internet. Make sure to


Serving Files with Python's SimpleHTTPServer Module, We will discuss how to configure your web server to make Python run as CGI, how This may be the most simple way to run CGI Scripts on Apache. installation path and its parameters (e.g. "C:\Python27\Python.exe %s %s"). discussed here, NGINX does not execute external programs like CGI directly. And if the web server is listening on a non-default port number (neither 80 nor 443), you must explicitly specify the port number like this: By now you should have all the necessary information to create an http web server in Python. So without further ado, let’s get started. Create a simple HTML file. Here is what we want to do.