A simple, dynamic, decorator-based HTTP server inspired by Node.js's Express. Supports trio, TLS and WebSockets.
Project description
dhttp
dhttp, which is pretty short for decorable HTTP, is a
simple, yet dynamic, HTTP server written for Python 3.
It is inspired by Node.js's Express library.
"Decorable" is a reference to how extensively decorators are used throughout this project. They are very useful, and are one of the nicest syntaxes for a callback system like this.
Example code
import dhttp
import random
app = dhttp.DHTTPServer(int(sys.argv[1]) if len(sys.argv) > 1 else 8005)
app.alias('/index', '/')
app.alias('/index.htm', '/')
app.alias('/index.html', '/')
test_index = """<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My first DHTTP server</title>
</head>
<body>
<p><h2>Congratulations!</h2></p>
<hr>
<p><b>dhttp {version}</b> is now running on your machine.</p>
<p>How about <i>{party}</i> to comemorate? :)</p>
</body>
</html>"""
party_stuff = [
'a bottle of wine', 'a bottle of champagne', 'a big party',
'THE party, just', 'THE party', 'lots of cats', 'partyception',
'balloons and cakes', 'a big-endian cake', 'lots of confetti',
'the Confetti-o-Tron 2000', 'HTTP juice', 'Spicy Bytes',
'antimatter', 'cats writing code', 'a smile breaking the 4th wall'
]
@app.get('/')
def serve_index(req, res):
res.end(test_index.format(
party = random.choice(party_stuff),
version = DHTTP_VERSION
))
@app.on_log
def print_log(log):
if log.request.get_header('X-Forwarded-For') is not None:
log.ip = log.request.get_header('X-Forwarded-For')
print(log, ' (forwarded)')
else:
print(log)
@app.serve_forever
def on_serve():
print(f" == Listening on port: {app.port} ==")
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
dhttp-1.3.3.tar.gz
(27.8 kB
view details)
Built Distribution
dhttp-1.3.3-py3-none-any.whl
(30.8 kB
view details)
File details
Details for the file dhttp-1.3.3.tar.gz
.
File metadata
- Download URL: dhttp-1.3.3.tar.gz
- Upload date:
- Size: 27.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.12.4 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.5.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
49fbbc5329602a83df252657ff6c7c11fa29df0df76b816273102dcab7a15eaa
|
|
MD5 |
788ac8ca5c16336439d7a1a38329669b
|
|
BLAKE2b-256 |
f484102227e6ff6711c6272ff4d10dc1b4576f09628cb4c1dc86dea37ac59437
|
File details
Details for the file dhttp-1.3.3-py3-none-any.whl
.
File metadata
- Download URL: dhttp-1.3.3-py3-none-any.whl
- Upload date:
- Size: 30.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.12.4 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.5.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
0deb9fbc232449500da6832898aba20c40a4bfc01b84e8be6faee45a1f6307fa
|
|
MD5 |
4c9756f134bd4ac71cc1bf37e193e8b3
|
|
BLAKE2b-256 |
8728ba3aad8d9261622415b988c43dc8d85e5678dcb33550d84991947ac55aa2
|