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
<?pyhpand?>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
exitandsys.exitterminate the script, not the whole serveratexitregistered 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-clicommand - CGI scripts by using the
pyhp-cgicommand - WSGI servers by using the
pyhp.wsgi.appssubmodule - 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.utilsubmodule
- 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:
$_SERVERasSERVER$_REQUESTasREQUEST$_GETasGET$_POSTasPOST$_COOKIEasCOOKIE$_FILESasFILEShttp_response_codeheaderheaders_listheader_removeheaders_sentheader_register_callbackwith an additionalreplacekeyword argument to register multiple callbackssetcookiewith an additionalsamesitekeyword argumentsetrawcookiealso with an additionalsamesitekeyword argumentregister_shutdown_functionwith reversed callback execution order (LIFO)opcache_compile_filewhich raises Exceptions instead of returningFalsewhen compilation failsopcache_invalidateopcache_is_script_cachedopcache_reset
- is valid toml
- is looked for in these locations (no merging takes place, the first file wins):
- the path given by the
-cor--configcli argument - the path pointed to by the
PYHPCONFIGenvironment variable ~/.config/pyhp.toml/etc/pyhp.toml
- the path given by the
- raises a
RuntimeErrorif 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-backendorpython3 -m pyhp.backendscli 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
PYHPCONFIGenviron variable or copy pyhp.toml to one of the config file locations to use the CLI commands
- execute
debian/build_deb.shin 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-tomlandpython3-werkzeugare installed to use the CLI commands - Important:
pyhp-backend clearwill 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
PYHPCONFIGenviron 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()