- most recent version 2024-03 is https://www.python.org/downloads/
- python 3.12 First released: 2023-10-02 End of support: 2028-10 Release schedule: PEP 693
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:
- write a webserver in python (it is actually not so complicated)
- install apache2 and forward web requests to python
- 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
- it is actually not so complicated:
- here is a more elaborate version:
- wget https://dwaves.de/dev/python/2024-05-python-standalone-eclipse-debuggable-webserver.tar.gz
- tar fxvz 2024-05-python-standalone-eclipse-debuggable-webserver.tar.gz
- python3 ./webserver_wsgi.py # start webserver
- then call: http://127.0.0.1:8021/index.py (dynamic) or http://127.0.0.1:8021/index.html (static)
- here is a more elaborate version:
- based on wsgiref
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 # 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 # what can be very handy # to mount /var/www/html to /home/user/projects/webroot
apt install bindfs; # install bindfs bindfs -u 1000 -g 1000 /var/www/html /home/user/projects/webroot # 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""")
# alternatively try:
vim /var/www/html/index.py
#!/usr/bin/env python3
""" this will allow to use inline python for dynamic website output :) """
__author__ = "user@domain.com"
import sys
sys.stdout = open(sys.stdout.fileno(), mode="w", encoding="utf-8", newline=None, buffering=1)
print("Content-Type: text/html")
print()
print("""
<html>
<head>
crazy html js css goes here
</head>
<body>
</body>
</html>
""")
# a possibly more interesting example
# that will also print all available environment variables
vim /var/www/html/index.py
#!/usr/bin/env python3 """ this will allow to use inline python for dynamic website output :) """ __author__ = "user@domain.com" # import sys # sys.stdout = open(sys.stdout.fileno(), mode="w", encoding="utf-8", newline=None, buffering=1) print("Content-Type: text/html") print() print(""" <html> <head> </head> <body> """) import os print("<h1>output all environment variales:</h1>") print("<ul>") for k, v in os.environ.items(): print("<li>") print(f'{k}={v}') print("</li>") print("</ul>") print(""" </body> </html> """) # make browser send a request with data: http://127.0.0.1/index.py?key1=value1&key2=value2 # it will output![]()
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?
- eclipse!
- PyCharm Community edition
- 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.
Speed?
as visible here, Python is (thanks to massive amounts of libraries + the community :D) a very versatible language, but it will never be as fast as C or C++
but there are possibilities to speed up performance: How to convert Python code to Cython (and speed up 100x)?
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!
