Skip to main content

socketwrench

A webserver based on socket.

Usage

import logging
from socketwrench import Server as SocketWrench, methods
from pathlib import Path

from socketwrench.tags import private, post, put, patch, delete

logging.basicConfig(level=logging.DEBUG)


class Sample:
    def hello(self):
        """A simple hello world function."""
        return "world"

    @methods("GET", "POST")  # do to the label, this will be accessible by both GET and POST requests
    def hello2(self, method):
        """A simple hello world function."""
        return "world"

    def _unserved(self):
        """This function will not be served."""
        return "this will not be served"
    
    @private
    def unserved(self):
        """This function will not be served."""
        return "this will not be served"

    @post
    def post(self, name):
        """This function will only be served by POST requests."""
        return f"hello {name}"
    
    @put
    def put(self, name):
        """This function will only be served by PUT requests."""
        return f"hello {name}"
    
    @patch
    def patch(self, name):
        """This function will only be served by PATCH requests."""
        return f"hello {name}"
    
    @delete
    def delete(self, name):
        """This function will only be served by DELETE requests."""
        return f"hello {name}"

    def echo(self, *args, **kwargs):
        if not args and not kwargs:
            return
        if args:
            if len(args) == 1:
                return args[0]
            return args
        elif kwargs:
            return kwargs
        return args, kwargs

    def string(self):
        return "this is a string"

    def html(self):
        return "<h1>hello world</h1><br><p>this is a paragraph</p>"

    def json(self):
        return {"x": 6, "y": 7}

    def file(self):
        return Path(__file__)

    def add(self, x: int, y: int):
        return x + y

    def client_addr(self, client_addr):
        return client_addr

    def headers(self, headers):
        return headers

    def query(self, query, *args, **kwargs):
        return query

    def body(self, body):
        return body

    def method(self, method):
        return method

    def route(self, route):
        return route

    def request(self, request):
        return request

    def everything(self, request, client_addr, headers, query, body, method, route, full_path):
        d = {
            "request": request,
            "client_addr": client_addr,
            "headers": headers,
            "query": query,
            "body": body,
            "method": method,
            "route": route,
            "full_path": full_path,
        }
        for k, v in d.items():
            print(k, v)
        return d


if __name__ == '__main__':
    s = Sample()
    SocketWrench(s, serve=True)

Download files

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

Source Distribution

socketwrench-0.2.1.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

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

socketwrench-0.2.1-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file socketwrench-0.2.1.tar.gz.

File metadata

  • Download URL: socketwrench-0.2.1.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for socketwrench-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a8b0672226f2c93393e9d4507da3ed5b126ad16fb56a5387e689bfa4dd615544
MD5 3fbdb645ec36d544c2db88ec750f83e4
BLAKE2b-256 1def4c2341a3003089f9077d5b0375691f510bcc280c7cb9e134e4fb0a7fc123

See more details on using hashes here.

File details

Details for the file socketwrench-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: socketwrench-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for socketwrench-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1da1198df6aded7588c83b8d95f9c2b6226a1a0987ec57f532881239319a6c12
MD5 170b61eeaf45f80e84f331ebd0a21fa0
BLAKE2b-256 a74d4801ff7e7147e80e74c125d165f1b917051bf855f243383456a1b2a107e5

See more details on using hashes here.

Release history Release notifications | RSS feed

2.3.1

2 files

2.3.0

2 files

2.2.10

2 files

2.2.9

2 files

2.2.8

2 files

2.2.7

2 files

2.2.6

2 files

2.2.5

2 files

2.2.4

2 files

2.2.3

2 files

2.2.2

2 files

2.2.1

2 files

2.2.0

2 files

2.1.2

2 files

2.1.1

2 files

2.1.0

2 files

2.0.4

2 files

2.0.3

2 files

2.0.1

2 files

2.0.0

2 files

1.9.3

2 files

1.9.2

2 files

1.9.1

2 files

1.9.0

2 files

1.8.9

2 files

1.8.8

2 files

1.8.7

2 files

1.8.6

2 files

1.8.5

2 files

1.8.4

2 files

1.8.3

2 files

1.8.2

2 files

1.8.1

2 files

1.8.0

2 files

1.7.0

2 files

1.6.0

2 files

1.4.0

2 files

1.3.0

2 files

1.2.0

2 files

1.1.1

2 files

This release

0.2.1 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page