A simple Python library for creating web servers and APIs using sockets
Project description
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)
Project details
Release history Release notifications | RSS feed
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 hashes)
Built Distribution
Close
Hashes for socketwrench-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1da1198df6aded7588c83b8d95f9c2b6226a1a0987ec57f532881239319a6c12 |
|
MD5 | 170b61eeaf45f80e84f331ebd0a21fa0 |
|
BLAKE2b-256 | a74d4801ff7e7147e80e74c125d165f1b917051bf855f243383456a1b2a107e5 |