web – How do I use python as a server-side language?

web – How do I use python as a server-side language?

What Nicarus said was a valid sugestion but I also recommend some other things.

First, for your development environment you wont need to use xampp or wamp or such things, python has its own HTTP server, although not the simplest thing to use, the libraries and frameworks I will explain next use that.

So, most python web developers dont use raw python to use python as their programming language for the web. Most developers use a framework or library of some kind. These frameworks range from being rather heavy and opinionated, like Django, to smaller ones like Flask. Most, if not all, of these frameworks provide some kind of easy and quick way to set up a development HTTP server for testing.

I would recommend looking up Django first since it has the most comprehensive tutorials and guides to get you started. Then, ones youre more comfortable in the Python language you can fairly easily use something else with less hand holding.

With django you can start here

Python can be a great side server language, but not in the way PHP is. It is highly recommended to use a framework, like flask.

In PHP you have different .php files (like index.php) that do a basic routing, but in python (with Flask and also some others frameworks) you must define the URI path within the function.

You can see here an example, from its webpage Flask

from flask import Flask
app = Flask(__name__)

def hello():
    return Hello World!

if __name__ == __main__:

And if you look for a API REST framework, take a look to Falcon

web – How do I use python as a server-side language?

For the purposes you mention, this current setup should work (at least for a while):

Apache Setup

Changes in httpd.conf

  1. Change denied to granted

<Directory />
AllowOverride none
Require all granted

  1. Look for Options +Indexes +FollowSynsLinks…….. and add +ExecCGI to that line.

Options +Indexes +FollowSymLinks +Multiviews +ExecCGI

  1. Look for AddHandler cgi-script .cgi and add .py to it

AddHandler cgi-script .cgi .py

Changes in httpd-vhosts.conf

<Directory {Your directory}/>
Options +Indexes +Includes +FollowSymLinks +MultiViews +ExecCGI
AllowOverride All
Require all granted


First line of Python code #!C:{somepath}python

You need to find the right path and bar convention ( /, , //…) for the first and commented line of code. In your example is: #!/Python/python

Run this script.py to find it

import sys

Paste the output next to #! and you should be able to test your test-code.py into your **www folder
If happens to be the case that it does not work, try different combinations of python, python.exe with different bar conventions like / // next to the #! line.

To display an HTML file add print (Content-type: text/htmlnn) on to your .py code.**

Full code

Note f on html_content = f<html>...

#!C:Program Files (x86)Python37-32python
#  -*- coding: utf-8 -*-
import cgi

stringvar = Hello variable
intvar = 30
html_content = f<html>
<head><title>My first Python CGI app</title></head>
<p>Hello, world!</p>
<p>This is a stringvar = {stringvar} </p>
<p>This is intvar = {intvar} </p>

print (Content-type: text/htmlnn)
print (html_content)

Most of the info is outdated or controversial

Totally agree. I hope this works!

Leave a Reply

Your email address will not be published. Required fields are marked *