Skip to main content

A tiny, programmable http-server crafting-framework that is built with security and simplicity in mind.

Project description

Sonoma

PyPI version

A tiny, programmable http-server crafting-framework that is built with security and simplicity in mind.



Setup

pip install sonoma


Basic Usage

Server

from sonoma import httpServer

server = httpServer('127.0.0.1', 8888)

server.run()

Browser

                Hello World!
This is the default webpage for Sonoma/1.0.x.

Basic Usage: Custom Response

Server

from sonoma import defaults, httpServer

server = httpServer('127.0.0.1', 8888)

defaults['defaultResponse'] = """ 
    <!DOCTYPE html><html><head>
    <style>html, body{ margin: 0 auto;text-align:center; }</style>
    </head><body>
    <h1 style=\"text-align:center;\">Hello World!</h1>
    <span>This is a modified version of the default webpage for %s.</span>
    </body></html>
    """ % defaults['serverName'] 

server.run()

Browser

                        Hello World!
This is a modified version of the default webpage for Sonoma/1.0.10.

Advanced Usage: Custom Handler

Server

from sonoma import httpServer, defaults
from http import HTTPStatus

# DEFINE A CUSTOM HANDLER
def myHandler(self, requestStatusLine, requestHeaders, requestBody, client_connection, client_address):
    """
    ## Supported Methods:
    - GET
    - HEAD
    """

    # LOG THE REQUEST
    sonomaPrint("Client: %s Request: %s" % (str(client_address), requestStatusLine,))

    headerStrings = []
    for header in requestHeaders:
        headerStrings.append("%s: %s\n" % (header[0], header[1]))   

    myCustomResponse = ("""
        <!DOCTYPE html><html><head>
        <style>html, body{ margin: 0 auto;text-align:center; }</style>
        </head><body>
        <h1 style=\"text-align:center;\">Hello World!</h1>
        <span>This is a custom response from %s.</span>
        <br/><br/>
        <span>Request Headers:</span>
        <br/>
        <textarea cols="100" rows="100" style="width: 75%%;height: 100%%;margin: 0 auto;">%s</textarea>
        </body></html>
    """ % (defaults['serverName'], "".join(headerStrings))).encode()

    # SERVE GET
    if requestStatusLine.split()[0].lower() == "get":
        responseStatusLine, responseHeaders = self.httpHeaders(HTTPStatus.OK, contentType="html")
        responseBody = myCustomResponse
        return (responseStatusLine, responseHeaders, responseBody)

    # SERVE HEAD
    elif requestStatusLine.split()[0].lower() == "head":   
        responseStatusLine, responseHeaders = self.httpHeaders(HTTPStatus.OK, contentType="text")
        return (responseStatusLine, responseHeaders, "")  

    # RESPOND WITH 405 STATUS - METHOD NOT ALLOWED
    else:
        responseStatusLine, responseHeaders = self.httpHeaders(HTTPStatus.METHOD_NOT_ALLOWED, contentType="text")
        return (responseStatusLine, responseHeaders, "")   


# INITIALIZE THE SERVER BUT SET OUR CUSTOM HANDLER BEFORE RUNNING.
server = httpServer('127.0.0.1', 8888)

server.set_handler(myHandler)

server.run()

Browser


Conclusion

  • Adding better documentation 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

sonoma-1.0.10.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

sonoma-1.0.10-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file sonoma-1.0.10.tar.gz.

File metadata

  • Download URL: sonoma-1.0.10.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for sonoma-1.0.10.tar.gz
Algorithm Hash digest
SHA256 ceec0961e84ad52033862b858577ac46c5c0e82f3a01e49c11a72165cf5c6fc2
MD5 4d18ad57bb49aea64f2a010626c4cc9a
BLAKE2b-256 971bdd95c78390e2a5c04d9fe641d2756d7514844c7b7395f2d9a0e5b7ed581a

See more details on using hashes here.

File details

Details for the file sonoma-1.0.10-py3-none-any.whl.

File metadata

  • Download URL: sonoma-1.0.10-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for sonoma-1.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 bf496589473f64f541ffc2d4ef513bc078e2601e53055c8c2bb3cb82e9319d61
MD5 3d5ec16eace11b76bf50bf07c38e3fae
BLAKE2b-256 0c34e774788a4b692824af078256d46e1f6cc1c1ef3725b3c249bfc71b09410e

See more details on using hashes here.

Supported by

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