How to incorporate python code on a website? [duplicate]

I am trying to use Python instead of PHP (just for educational purposes). I am testing my webpages on XAMPP and I already added python and cgi to configs. I managed to make a very basic webpage with this code

#!/Python/python

print("Content-Type: text/plain;charset=utf-8")
print()

print("Hello World!")

Though, this is it. I can't find any info on how exactly do I serve webpages with Python 3+. Most of the info is outdated or controversial. Is there an up-to-date guide on how to use Python as a server-side language?


Solution 1:

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

First, for your development environment you won't need to use xampp or wamp or such things, python has it's 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 don't 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 you're more comfortable in the Python language you can fairly easily use something else with less hand holding.

With django you can start here

Solution 2:

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__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

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

Solution 3:

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 </Directory>

  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 </Directory>


example.py

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 print(sys.executable)

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/html\n\n') on to your .py code.**

Full code
Note f on html_content = f'''<html>...

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

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

print ('Content-type: text/html\n\n')
print (html_content)

"Most of the info is outdated or controversial"

Totally agree. I hope this works!