PyHP is a package that allows you to embed Python code like PHP code into HTML and other text files. A script is called either by the configuration of the web server or a shebang and communicates with the web server via WSGI.
- Parser for embedding python Code in HTML
- a bunch of PHP features implemented in python
- modular structure to allow the use of features outside of the interpreter
- automatic code alignment for improved readability
- caching
- like PHP, Python code is contained within the
<?pyhp
and?>
tags - code sections are allowed to have a starting indentation for better readability inside (for example) HTML files
- unlike PHP, each code section of code must have a valid syntax of its own
- if-Statements or loops can not span multiple code sections
- module level constants are set and allow for source introspection if the backend supports it
exit
andsys.exit
terminate the script, not the whole serveratexit
registered functions dont get called until server shutdown in WSGI mode- since try statements can't span multiple code sections cleanup actions should be executed by
register_shutdown_function
- can be used for
- CLI scripts with the
pyhp-cli
command - CGI scripts by using the
pyhp-cgi
command - WSGI servers by using the
pyhp.wsgi.apps
submodule - if no name is given, the program is reading from stdin, else it is using the name to load code from the configured backend
- execute code and capture its output
- provide the code with an interface
- are available for single and multi-threaded environments
- can be constructed by factories contained in the
pyhp.wsgi.util
submodule
- act as an interface between the WSGI gateway and the script
- are available as thin WSGI wrappers or PHP-style interfaces
- the following PHP features are available:
$_SERVER
asSERVER
$_REQUEST
asREQUEST
$_GET
asGET
$_POST
asPOST
$_COOKIE
asCOOKIE
$_FILES
asFILES
http_response_code
header
headers_list
header_remove
headers_sent
header_register_callback
with an additionalreplace
keyword argument to register multiple callbackssetcookie
with an additionalsamesite
keyword argumentsetrawcookie
also with an additionalsamesite
keyword argumentregister_shutdown_function
with reversed callback execution order (LIFO)opcache_compile_file
which raises Exceptions instead of returningFalse
when compilation failsopcache_invalidate
opcache_is_script_cached
opcache_reset
- is valid toml
- is looked for in these locations (no merging takes place, the first file wins):
- the path given by the
-c
or--config
cli argument - the path pointed to by the
PYHPCONFIG
environment variable ~/.config/pyhp.toml
/etc/pyhp.toml
- the path given by the
- raises a
RuntimeError
if not found
- implement code retrieval or decorate other backends to add i.a. caching
- act as containers for CodeSources
- form a hierarchy configured in pyhp.toml
- are contained inside
pyhp.backends
- can be interacted with via the
pyhp-backend
orpython3 -m pyhp.backends
cli commands
This section shows you how to install PyHP on your computer. If you want to use pyhp scripts on your website by CGI you have to additionally enable CGI in your webserver.
- build the pyhp-core python package with
python3 setup.py bdist_wheel
- Done! You can now install the wheel contained in the dist directory with pip
- Optional: set the
PYHPCONFIG
environ variable or copy pyhp.toml to one of the config file locations to use the CLI commands
- execute
debian/build_deb.sh
in the root directory of the project. - Done! You can now install the debian package with
sudo dpkg -i python3-pyhp-core_{version}-1_all.deb
- Optional: check if the recommended packages
python3-toml
andpython3-werkzeug
are installed to use the CLI commands - Important:
pyhp-backend clear
will be executed on uninstall or upgrade if the backend is a cache, remember this when using paths containing~
for the file cache
- install the pyhp-core python package
- set the
PYHPCONFIG
environ variable or copy pyhp.toml to one of the config file locations - Done! You can now use the
pyhp-*
commands
import sys
import re
import tempfile
from wsgiref.simple_server import make_server
from pyhp.compiler import parsers, util, generic
from pyhp.backends.files import Directory
from pyhp.wsgi.apps import ConcurrentWSGIApp
from pyhp.wsgi.proxys import LocalStackProxy
from pyhp.wsgi.interfaces.php import PHPWSGIInterfaceFactory
from pyhp.wsgi.interfaces.phputils import UploadStreamFactory
compiler = util.Compiler(
parsers.RegexParser(
re.compile(r"<\?pyhp\s"),
re.compile(r"\s\?>")
),
util.Dedenter(
generic.GenericCodeBuilder(-1)
)
)
interface_factory = PHPWSGIInterfaceFactory(
200,
[("Content-type", "text/html; charset=\"UTF-8\"")],
None,
("GET", "POST", "COOKIE"),
8000000,
UploadStreamFactory(
tempfile.gettempdir(),
20
)
)
sys.stdout = proxy = LocalStackProxy(sys.stdout)
with Directory(".", compiler) as backend:
with ConcurrentWSGIApp("tests/embedding/syntax.pyhp", backend, proxy, interface_factory) as app:
with make_server("", 8000, app) as httpd:
httpd.serve_forever()
from wsgiref.simple_server import make_server
import toml
from pyhp.wsgi.util import ConcurrentWSGIAppFactory
config = toml.load("pyhp.toml")
with ConcurrentWSGIAppFactory.from_config(config) as factory:
with factory.app("tests/embedding/syntax.pyhp") as app:
with make_server("", 8000, app) as httpd:
httpd.serve_forever()