HTTP Server API Reference¶
Auto-generated API documentation for the HTTP Server module.
httpserver
¶
Zero-dependency async HTTP server with decorator-based routing.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Async HTTP/1.1 server built on asyncio.start_server(). Supports
decorator-based routing, JSON request/response, static file serving,
streaming responses (SSE), and graceful shutdown.
Usage::
from httpserver import App, JSONResponse
app = App()
@app.route("/status")
async def status(request):
return JSONResponse({"state": "idle"})
@app.route("/echo", methods=["POST"])
def echo(request):
return JSONResponse(request.json())
app.run(host="127.0.0.1", port=8000)
HTTPException
¶
Bases: Exception
HTTP error that maps to a specific status code.
Raise directly or via :func:abort to short-circuit request handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status_code
|
int
|
HTTP status code (e.g. 404, 500). |
required |
message
|
str | None
|
Optional human-readable error message. |
None
|
Source code in httpserver/httpserver.py
Request
¶
Parsed HTTP request.
Attributes:
| Name | Type | Description |
|---|---|---|
method |
Uppercase HTTP method (GET, POST, ...). |
|
path |
URL path, percent-decoded. |
|
query_string |
Raw query string (without leading |
|
query_params |
dict[str, list[str]]
|
Parsed query string as |
headers |
Case-insensitive header dict (keys stored lowercase). |
|
body |
Raw request body bytes. |
|
path_params |
dict[str, Any]
|
Parameters extracted from the route pattern. |
client_addr |
Client |
|
app |
Reference to the :class: |
Source code in httpserver/httpserver.py
Response
¶
HTTP response with a fixed body.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
body
|
bytes | str
|
Response body (bytes or str). |
b''
|
status_code
|
int
|
HTTP status code. |
200
|
headers
|
dict[str, str] | None
|
Extra response headers. |
None
|
content_type
|
str | None
|
Shorthand for |
None
|
Source code in httpserver/httpserver.py
JSONResponse
¶
Bases: Response
Response serialized as JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Python object to serialize. |
required |
status_code
|
int
|
HTTP status code. |
200
|
headers
|
dict[str, str] | None
|
Extra response headers. |
None
|
Source code in httpserver/httpserver.py
StreamingResponse
¶
HTTP response streamed from an async generator.
Writes chunks using Transfer-Encoding: chunked unless
content_type is text/event-stream (SSE), in which case raw
bytes are flushed directly for maximum compatibility with SSE clients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generator
|
AsyncIterator[bytes | str]
|
Async iterator yielding |
required |
status_code
|
int
|
HTTP status code. |
200
|
headers
|
dict[str, str] | None
|
Extra response headers. |
None
|
content_type
|
str
|
MIME type (default |
'application/octet-stream'
|
Source code in httpserver/httpserver.py
FileResponse
¶
Bases: Response
Response serving a file from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the file. |
required |
content_type
|
str | None
|
MIME type (auto-detected from extension if |
None
|
status_code
|
int
|
HTTP status code. |
200
|
Source code in httpserver/httpserver.py
App
¶
Async HTTP server application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_body_size
|
int
|
Maximum request body size in bytes. |
DEFAULT_MAX_BODY_SIZE
|
read_timeout
|
float
|
Timeout for reading a single request (seconds). |
DEFAULT_READ_TIMEOUT
|
Example::
app = App()
@app.get("/hello")
async def hello(request):
return {"message": "Hello, world!"}
app.run()
Source code in httpserver/httpserver.py
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 | |
route(url_pattern, methods=None)
¶
Register a route handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_pattern
|
str
|
URL pattern with optional parameters. |
required |
methods
|
list[str] | None
|
HTTP methods to handle (e.g. |
None
|
Example::
@app.route("/users/<int:id>", methods=["GET", "POST"])
async def user(request, id):
return {"id": id}
Source code in httpserver/httpserver.py
get(path)
¶
post(path)
¶
put(path)
¶
delete(path)
¶
patch(path)
¶
static(url_prefix, directory)
¶
Register a directory for static file serving.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_prefix
|
str
|
URL prefix (e.g. |
required |
directory
|
str
|
Filesystem path to the directory. |
required |
Example::
app.static("/assets", "./public")
Source code in httpserver/httpserver.py
before_request(handler)
¶
Register a before-request hook.
The hook receives (request) and may return a Response to
short-circuit the route handler.
Source code in httpserver/httpserver.py
after_request(handler)
¶
Register an after-request hook.
The hook receives (request, response) and may return a new
or modified Response.
Source code in httpserver/httpserver.py
errorhandler(code_or_exc)
¶
Register an error handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code_or_exc
|
int | type
|
HTTP status code (int) or exception class. |
required |
The handler receives (request, exception) and must return a
Response.
Source code in httpserver/httpserver.py
run(host=DEFAULT_HOST, port=DEFAULT_PORT)
¶
Start the server (blocking).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
Bind address. |
DEFAULT_HOST
|
port
|
int
|
Bind port. Use |
DEFAULT_PORT
|
Source code in httpserver/httpserver.py
shutdown()
¶
Request a graceful server shutdown.
Safe to call from a request handler.
abort(status_code, message=None)
¶
Raise an :class:HTTPException with the given status code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status_code
|
int
|
HTTP status code. |
required |
message
|
str | None
|
Optional error message. |
None
|