Skip to main content

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

Project description

Sonoma

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.

Advanced Usage: Default 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
    """

    print("Client: %s\n" % str(client_address))

    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)) 

    # 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.9.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

sonoma-1.0.9-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sonoma-1.0.9.tar.gz
  • Upload date:
  • Size: 4.7 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.9.tar.gz
Algorithm Hash digest
SHA256 88a1efc96853b66968e8812b85492830ac9e83280ae665519f3d62a403d6eccb
MD5 8e55eb5525c997f33008df990ed43bf7
BLAKE2b-256 9a029ce5739729ed8f64326e994f3e99a06f0ffc6bc6d9adc57172b307f30fea

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sonoma-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 5.5 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.9-py3-none-any.whl
Algorithm Hash digest
SHA256 2ca96fe58fd66d843e244c248c1eb89eb5a4ad3e3c2b49d0ef2200204e557b4d
MD5 b6bc2831a2f2812da3486cedfe2919da
BLAKE2b-256 b09eb67ad5a1f8d12b882af5ac116755c9def4d6dfb6dd99cecf7c3ba207f282

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