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)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8b0672226f2c93393e9d4507da3ed5b126ad16fb56a5387e689bfa4dd615544
|
|
| MD5 |
3fbdb645ec36d544c2db88ec750f83e4
|
|
| BLAKE2b-256 |
1def4c2341a3003089f9077d5b0375691f510bcc280c7cb9e134e4fb0a7fc123
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1da1198df6aded7588c83b8d95f9c2b6226a1a0987ec57f532881239319a6c12
|
|
| MD5 |
170b61eeaf45f80e84f331ebd0a21fa0
|
|
| BLAKE2b-256 |
a74d4801ff7e7147e80e74c125d165f1b917051bf855f243383456a1b2a107e5
|