Skip to main content

display colors in console

Project description

PyWSGIRef

easy server-setup

Advantage

Many web services offer simple ways to set up a WSGI webserver.
The built-in WSGI server in Python is, however, not very easy to use.
PyWSGIRef provides a simple way to set up a WSGI server with minimal code.

Also, a problem with many of those web services is that they aren't able to make web requests
and it's very slow to read files once the server is running.
PyWSGIRef solves this problem by providing a simple way to load PyHTML files from the web or from the local filesystem
before the server is running.

PyHTML files are HTML files that can contain {}-s for Python formatting or
(upcoming) code blocks or shortened HTML, which can be used to create dynamic HTML content.
PyWSGIRef also provides a simple way to decode these.

Installation

Using pip

You can install PyWSGIRef via pip using

  • in a commandline:

  • py -m pip install PyWSGIRef
    

  • in a python script:

  • import os
    os.system('py -m pip install PyWSGIRef')
    

    Usage

    Setting up the WSGI server

    from PyWSGIRef import *
    
    # Create a WSGI application object
    def simple_app(path: str) -> str:
    	return f"Hello, you visited {path}!"
    application = makeWSGIApp(simple_app)
    
    # Create a WSGI server
    server = makeWSGIServer(application, port=8000)
    server.serve_forever()
    

    The makeWSGIApp function requires a callable that takes a single argument (the path) and returns a string response.
    The makeWSGIServer function creates a WSGI server that listens on the specified port (default is 8000)
    and calls the application object with the environ.
    You can also use your own application object instead of the one created by makeWSGIApp.

    Loading PyHTML

    PyWSGIRef also provides a simple way to load PyHTML files:

    from PyWSGIRef import *
    
    # load from file
    html = loadFromFile('index.pyhtml')
    
    # load from web
    html = loadFromWeb('https://example.com/index.pyhtml')
    

    This is useful for serving dynamic HTML content in your WSGI application.
    The funcs are callable unless you set up a server.

    Using Templates

    PyWSGIRef also provides a simple way to use templates in your WSGI application:

    from PyWSGIRef import *
    
    # add template to template dictionary
    # -> saves as PyHTML object
    addSchablone("index", loadedPyHTMLFile)
    
    # load template from dictionary
    # get HTML code from PyHTML object
    html = SCHABLONEN["index"].decoded()
    # use Python formatting
    serverPageContent = html.format("Hello, World!")
    

    Note that you may only use the addSchablone function before you set up a server.

    Default HTML templates

    PyWSGIRef comes with some default HTML templates that you can use in your WSGI application.
    You can access them via the PyWSGIRef.defaults:

    from PyWSGIRef import *
    
    # load default HTML template
    addSchablone("hello", HELLO_WORLD)
    addSchablone("error", ERROR)
    

    main script

    You may produce a simple WSGI server by using either:

    from PyWSGIRef import *
    
    main()
    

    or (from a commandline)

    py -m PyWSGIRef
    

    Others

    Use the following to get information about your release and the author of the module:

    pycols.about()
    

    More coming soon

    Project details


    Download files

    Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

    Source Distribution

    pywsgiref-1.0.1.tar.gz (5.6 kB view details)

    Uploaded Source

    Built Distribution

    If you're not sure about the file name format, learn more about wheel file names.

    pywsgiref-1.0.1-py3-none-any.whl (6.6 kB view details)

    Uploaded Python 3

    File details

    Details for the file pywsgiref-1.0.1.tar.gz.

    File metadata

    • Download URL: pywsgiref-1.0.1.tar.gz
    • Upload date:
    • Size: 5.6 kB
    • Tags: Source
    • Uploaded using Trusted Publishing? No
    • Uploaded via: twine/4.0.0 CPython/3.11.0

    File hashes

    Hashes for pywsgiref-1.0.1.tar.gz
    Algorithm Hash digest
    SHA256 d49c4122e678d0b2a80bcbd62af30503aa67c6d3b4f0259536a57a162e673e89
    MD5 cf2903de12147ac2bc00448f46fbfe4e
    BLAKE2b-256 2ae981ab0be8343e7f3924dbe626405e9e8b77ebd82d64ae755dd775cafceaf1

    See more details on using hashes here.

    File details

    Details for the file pywsgiref-1.0.1-py3-none-any.whl.

    File metadata

    • Download URL: pywsgiref-1.0.1-py3-none-any.whl
    • Upload date:
    • Size: 6.6 kB
    • Tags: Python 3
    • Uploaded using Trusted Publishing? No
    • Uploaded via: twine/4.0.0 CPython/3.11.0

    File hashes

    Hashes for pywsgiref-1.0.1-py3-none-any.whl
    Algorithm Hash digest
    SHA256 17e2b7fdf5db9c2d07c5b71a7201490852e3404799bf06283b5767f42bec3c9d
    MD5 51032faef0212fff5b8a458b9e913130
    BLAKE2b-256 21107a9559e99f3cb0a7f90077d127ba15c5515952068ecbb137da6bf4d608bb

    See more details on using hashes here.

    Supported by

    AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page