python for automation:

So for performance critical tasks python is actual just the “controller” that then calls C or or C++ or RUST or other binaries that can do the bulk data processing FAST.

So it is highly used for automation, that’s why RedHat’s yum and Ansible is using it.

python + the web:

There are actually at least 3 ways to make Python web compatible:

  1. write a webserver in python (it is actually not so complicated)
  2. install apache2 and forward web requests to python
  3. frameworks: flask, django

To be considered: when PHP scripts are run via Apache2: the timeout (usually maximum 30sec) after which the process is simply killed to not overload the multi threaded webserver.

When run from command line (CLI) there is no such timeout and PHP scripts can run for longer.

1. write a webserver in python

2. install apache2 and forward web requests to python

hostnamectl; # tested on 
Virtualization: kvm
Operating System: Ubuntu 22.04.4 LTS 
Kernel: Linux 5.15.0-94-generic
Architecture: x86-64
su - root
apt update
apt install python3 apache2

# make python3 the default

ln -sv /usr/bin/python3 /usr/bin/py
ln -sv /usr/bin/python3 /usr/bin/python

# disable multi threading (for test system)
a2dismod mpm_event
a2enmod mpm_prefork cgi

# modify add this to default site config file
vim /etc/apache2/sites-enabled/000-default.conf
<VirtalHost>
...
<Directory /var/www/html >
Options +ExecCGI
DirectoryIndex index.py
</Directory>
AddHandler cgi-script .py
</VirtualHost>

# if mysql access is required
apt install python3-pip
# it will complain like this env is "externally managed"
# which means, look for an deb package
pip3 install pymysql

apt search pymysql
python-aiomysql-doc/stable,stable 0.1.1-2 all
library for accessing a MySQL database from the asyncio (common documentation)

python-pymysql-doc/stable,stable 1.0.2-2 all
Pure-Python MySQL driver - doc

python3-aiomysql/stable,stable 0.1.1-2 all
library for accessing a MySQL using asyncio (Python 3)

python3-pymysql/stable,stable 1.0.2-2 all
Pure-Python MySQL Driver - Python 3.x
# is (probably) what dev wants
apt install python3-pymysql

# test apache2 config is ok
apachectl configtest

# this message is okay, as the dev is running a local and not publicly accessible webserver
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message

# restart apache2 to apply config
systemctl restart apache2

# make sure apache2 starts on boot
systemctl enable apache2

# check everything is running
systemctl status apache2

# create first python-web-test-file
vim /var/www/html/index.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys

print("Content-Type: text/html") # apache2-cgi: if this line is commented out, instead of running file.py, browser will download file.py
print()
print("""
<html>
<head>
</head>
<body>
<h1>it works! apache2 is currently running python version:""" + sys.version + """</h1>
</body>
</html""")
# give apache2 default user access
chown -R www-data: /var/www/
chmod ug+x /var/www/html/*.py

# kid u not it's probably a good idea to reboot the system now
reboot

now start browser (firefox)

to go-to-ip-of-server (or) if graphical user interface is installed on server: http://localhost/index.html (apache2 test page)

now let’s test if it is running python 🙂

try browsing to:  http://localhost/index.py

if the developer sees this:

great! 🙂 celebrate!

python enabled webserver works 🙂

if not: half so wild

open a second terminal and become root and launch this one-line-all-logs-live-monitoring-debugger.

it shall point to where the problem is. (typo in apache config, *.py scripts not marked executable… “permission denied”)

performance

while the above way was tested and worked, to increase performance:

su - root
apt update
apt install libapache2-mod-python

but that ain’t working how it should

IDEs?

https://dwaves.de/2024/01/23/gnu-linux-ubuntu-but-should-work-the-same-on-debian-how-to-setup-eclipse-python-development-ide-pydev-in-2024-including-step-debugging-and-all/

optional PyCharm Community edition or vscode it works fine with GNU Linux Debian

while vscode is one of the few M$ (true?) open source projects (written in TypeScript (which translates to JavaScript before runtime)) (languages, written in languages, that write languages)

it is actually it might infringe privacy wise:Data Collection. The software may collect information about the user and how the user uses the software, and send that to Microsoft unless it is on a internet-disconnected vm.

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin