mini-aiohttp 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 mini-aiohttp contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,86 @@
1
+ Metadata-Version: 2.4
2
+ Name: mini-aiohttp
3
+ Version: 0.1.0
4
+ Summary: A small, dependency-free async HTTP/1.1 client and server built on asyncio
5
+ License: MIT
6
+ Requires-Python: >=3.9
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Provides-Extra: test
10
+ Requires-Dist: pytest>=7; extra == "test"
11
+ Requires-Dist: pytest-asyncio>=0.21; extra == "test"
12
+ Dynamic: license-file
13
+
14
+ # mini-aiohttp
15
+
16
+ A small, dependency-free async HTTP/1.1 client and server built on Python's `asyncio`.
17
+
18
+ ## Features
19
+
20
+ - Async client with keep-alive connection pooling per host
21
+ - Async server with routing, keep-alive, and request body limits
22
+ - Connection cap: new connections get `503` when the limit is reached, so memory stays bounded
23
+ - Timeouts on requests and idle connections
24
+ - Chunked response parsing (client) and `Content-Length` request bodies (server)
25
+ - HTTPS on the client side
26
+
27
+ ## Install
28
+
29
+ ```bash
30
+ pip install . # from source
31
+ pip install .[test] # with test dependencies
32
+ ```
33
+
34
+ ## Quick start
35
+
36
+ ```python
37
+ import asyncio
38
+ from mini_aiohttp import Server, Response, ClientSession
39
+
40
+ app = Server(max_connections=100_000)
41
+
42
+ @app.route("/")
43
+ async def index(req):
44
+ return Response(200, "Hello")
45
+
46
+ @app.route("/echo", method="POST")
47
+ async def echo(req):
48
+ return Response.json({"got": req.json()})
49
+
50
+ async def main():
51
+ server = asyncio.create_task(app.serve_forever("127.0.0.1", 8080))
52
+ await asyncio.sleep(0.2)
53
+ async with ClientSession() as s:
54
+ r = await s.get("http://127.0.0.1:8080/")
55
+ print(r.status, r.body.decode())
56
+ server.cancel()
57
+
58
+ asyncio.run(main())
59
+ ```
60
+
61
+ ## Running tests
62
+
63
+ ```bash
64
+ pip install .[test]
65
+ pytest
66
+ ```
67
+
68
+ ## Scaling to many connections
69
+
70
+ A single Python process can hold tens of thousands of idle keep-alive connections. Reaching 100k needs OS and deployment tuning:
71
+
72
+ 1. **File descriptor limit.** Each socket uses one. Raise it before starting: `ulimit -n 200000`. The server logs a warning if the limit is too low.
73
+ 2. **Listen backlog.** Raise the kernel queue: `sysctl -w net.core.somaxconn=65535`. The server already requests a backlog of 4096.
74
+ 3. **Client-side ports.** One client machine opening outbound connections to a single `ip:port` is limited by ephemeral ports (about 28k to 64k). For more, spread load across several source IPs or client hosts.
75
+ 4. **Multiple processes.** Python runs one event loop per process. Start several workers with `Server.start(..., reuse_port=True)` on Linux to use all CPU cores.
76
+ 5. **Measure it.** Test with a load tool such as `wrk`, `oha`, or `locust` on your own hardware before you rely on any number.
77
+
78
+ ## Limits (by design, v0.1)
79
+
80
+ - No HTTPS server (put a reverse proxy such as nginx in front).
81
+ - No chunked request bodies on the server.
82
+ - No HTTP/2 or WebSockets.
83
+
84
+ ## License
85
+
86
+ MIT
@@ -0,0 +1,73 @@
1
+ # mini-aiohttp
2
+
3
+ A small, dependency-free async HTTP/1.1 client and server built on Python's `asyncio`.
4
+
5
+ ## Features
6
+
7
+ - Async client with keep-alive connection pooling per host
8
+ - Async server with routing, keep-alive, and request body limits
9
+ - Connection cap: new connections get `503` when the limit is reached, so memory stays bounded
10
+ - Timeouts on requests and idle connections
11
+ - Chunked response parsing (client) and `Content-Length` request bodies (server)
12
+ - HTTPS on the client side
13
+
14
+ ## Install
15
+
16
+ ```bash
17
+ pip install . # from source
18
+ pip install .[test] # with test dependencies
19
+ ```
20
+
21
+ ## Quick start
22
+
23
+ ```python
24
+ import asyncio
25
+ from mini_aiohttp import Server, Response, ClientSession
26
+
27
+ app = Server(max_connections=100_000)
28
+
29
+ @app.route("/")
30
+ async def index(req):
31
+ return Response(200, "Hello")
32
+
33
+ @app.route("/echo", method="POST")
34
+ async def echo(req):
35
+ return Response.json({"got": req.json()})
36
+
37
+ async def main():
38
+ server = asyncio.create_task(app.serve_forever("127.0.0.1", 8080))
39
+ await asyncio.sleep(0.2)
40
+ async with ClientSession() as s:
41
+ r = await s.get("http://127.0.0.1:8080/")
42
+ print(r.status, r.body.decode())
43
+ server.cancel()
44
+
45
+ asyncio.run(main())
46
+ ```
47
+
48
+ ## Running tests
49
+
50
+ ```bash
51
+ pip install .[test]
52
+ pytest
53
+ ```
54
+
55
+ ## Scaling to many connections
56
+
57
+ A single Python process can hold tens of thousands of idle keep-alive connections. Reaching 100k needs OS and deployment tuning:
58
+
59
+ 1. **File descriptor limit.** Each socket uses one. Raise it before starting: `ulimit -n 200000`. The server logs a warning if the limit is too low.
60
+ 2. **Listen backlog.** Raise the kernel queue: `sysctl -w net.core.somaxconn=65535`. The server already requests a backlog of 4096.
61
+ 3. **Client-side ports.** One client machine opening outbound connections to a single `ip:port` is limited by ephemeral ports (about 28k to 64k). For more, spread load across several source IPs or client hosts.
62
+ 4. **Multiple processes.** Python runs one event loop per process. Start several workers with `Server.start(..., reuse_port=True)` on Linux to use all CPU cores.
63
+ 5. **Measure it.** Test with a load tool such as `wrk`, `oha`, or `locust` on your own hardware before you rely on any number.
64
+
65
+ ## Limits (by design, v0.1)
66
+
67
+ - No HTTPS server (put a reverse proxy such as nginx in front).
68
+ - No chunked request bodies on the server.
69
+ - No HTTP/2 or WebSockets.
70
+
71
+ ## License
72
+
73
+ MIT
@@ -0,0 +1,7 @@
1
+ """mini-aiohttp: a small, dependency-free async HTTP/1.1 client and server."""
2
+ from .client import ClientError, ClientSession
3
+ from .response import Request, Response
4
+ from .server import Server
5
+
6
+ __version__ = "0.1.0"
7
+ __all__ = ["ClientSession", "ClientError", "Server", "Request", "Response"]
@@ -0,0 +1,196 @@
1
+ """Async HTTP/1.1 client with keep-alive connection pooling per host."""
2
+ from __future__ import annotations
3
+
4
+ import asyncio
5
+ import json as _json
6
+ from collections import defaultdict, deque
7
+ from urllib.parse import urlsplit
8
+
9
+ from .response import Response
10
+
11
+
12
+ class ClientError(Exception):
13
+ pass
14
+
15
+
16
+ class ClientSession:
17
+ def __init__(
18
+ self,
19
+ *,
20
+ timeout: float = 10.0,
21
+ max_per_host: int = 1000,
22
+ max_idle_per_host: int = 100,
23
+ user_agent: str = "mini-aiohttp/0.1.0",
24
+ ):
25
+ self.timeout = timeout
26
+ self.max_per_host = max_per_host
27
+ self.max_idle_per_host = max_idle_per_host
28
+ self.user_agent = user_agent
29
+ self._idle: dict = defaultdict(deque) # key -> deque of (reader, writer)
30
+ self._limits: dict = {} # key -> Semaphore
31
+ self._closed = False
32
+
33
+ async def __aenter__(self):
34
+ return self
35
+
36
+ async def __aexit__(self, *exc):
37
+ await self.close()
38
+
39
+ async def get(self, url: str, **kwargs) -> Response:
40
+ return await self.request("GET", url, **kwargs)
41
+
42
+ async def post(self, url: str, **kwargs) -> Response:
43
+ return await self.request("POST", url, **kwargs)
44
+
45
+ async def request(
46
+ self,
47
+ method: str,
48
+ url: str,
49
+ *,
50
+ headers: dict | None = None,
51
+ data=None,
52
+ json=None,
53
+ timeout: float | None = None,
54
+ ) -> Response:
55
+ if self._closed:
56
+ raise ClientError("session is closed")
57
+ if json is not None:
58
+ data = _json.dumps(json).encode("utf-8")
59
+ headers = {"Content-Type": "application/json", **(headers or {})}
60
+ elif isinstance(data, str):
61
+ data = data.encode("utf-8")
62
+
63
+ parts = urlsplit(url)
64
+ if parts.scheme not in ("http", "https") or not parts.hostname:
65
+ raise ValueError(f"Unsupported URL: {url!r}")
66
+ tls = parts.scheme == "https"
67
+ port = parts.port or (443 if tls else 80)
68
+ key = (tls, parts.hostname, port)
69
+ host_header = parts.hostname if port in (80, 443) else f"{parts.hostname}:{port}"
70
+ target = (parts.path or "/") + (f"?{parts.query}" if parts.query else "")
71
+ method = method.upper()
72
+ raw = self._build_request(method, target, host_header, headers, data)
73
+
74
+ limit = self._limits.get(key)
75
+ if limit is None:
76
+ limit = self._limits[key] = asyncio.Semaphore(self.max_per_host)
77
+ async with limit:
78
+ return await asyncio.wait_for(
79
+ self._roundtrip(key, raw, method), timeout or self.timeout
80
+ )
81
+
82
+ def _build_request(self, method, target, host_header, headers, body) -> bytes:
83
+ h = {"Host": host_header, "User-Agent": self.user_agent, "Accept": "*/*"}
84
+ if body is not None:
85
+ h["Content-Length"] = str(len(body))
86
+ h.update(headers or {})
87
+ lines = [f"{method} {target} HTTP/1.1"] + [f"{k}: {v}" for k, v in h.items()]
88
+ return "\r\n".join(lines).encode("latin-1") + b"\r\n\r\n" + (body or b"")
89
+
90
+ # ---------- connection pool ----------
91
+ async def _connect(self, key):
92
+ tls, host, port = key
93
+ return await asyncio.open_connection(
94
+ host, port,
95
+ ssl=True if tls else None,
96
+ server_hostname=host if tls else None,
97
+ )
98
+
99
+ def _take_idle(self, key):
100
+ pool = self._idle.get(key)
101
+ while pool:
102
+ reader, writer = pool.pop()
103
+ if not writer.is_closing():
104
+ return reader, writer
105
+ return None
106
+
107
+ def _put_idle(self, key, conn):
108
+ pool = self._idle[key]
109
+ if len(pool) < self.max_idle_per_host:
110
+ pool.append(conn)
111
+ else:
112
+ conn[1].close()
113
+
114
+ async def _roundtrip(self, key, raw: bytes, method: str) -> Response:
115
+ conn = self._take_idle(key)
116
+ if conn is not None:
117
+ try:
118
+ return await self._exchange(key, conn, raw, method)
119
+ except (ConnectionError, asyncio.IncompleteReadError):
120
+ pass # stale keep-alive socket closed by server: retry on a fresh one
121
+ conn = await self._connect(key)
122
+ return await self._exchange(key, conn, raw, method)
123
+
124
+ async def _exchange(self, key, conn, raw: bytes, method: str) -> Response:
125
+ reader, writer = conn
126
+ try:
127
+ writer.write(raw)
128
+ await writer.drain()
129
+ response, reusable = await self._read_response(reader, method)
130
+ except BaseException:
131
+ writer.close()
132
+ raise
133
+ if reusable:
134
+ self._put_idle(key, conn)
135
+ else:
136
+ writer.close()
137
+ return response
138
+
139
+ # ---------- response parsing ----------
140
+ @staticmethod
141
+ async def _read_response(reader, method: str):
142
+ status_line = await reader.readuntil(b"\r\n")
143
+ try:
144
+ version, code, *_ = status_line.decode("latin-1").rstrip("\r\n").split(" ", 2)
145
+ status = int(code)
146
+ except ValueError:
147
+ raise ClientError("Malformed status line")
148
+
149
+ headers: dict = {}
150
+ while True:
151
+ line = await reader.readuntil(b"\r\n")
152
+ if line == b"\r\n":
153
+ break
154
+ name, sep, value = line.decode("latin-1").partition(":")
155
+ if not sep:
156
+ raise ClientError("Malformed header")
157
+ headers[name.strip().lower()] = value.strip()
158
+
159
+ connection = headers.get("connection", "").lower()
160
+ if version == "HTTP/1.1":
161
+ reusable = "close" not in connection
162
+ else:
163
+ reusable = "keep-alive" in connection
164
+
165
+ if method == "HEAD" or status in (204, 304):
166
+ body = b""
167
+ elif "chunked" in headers.get("transfer-encoding", "").lower():
168
+ body = await ClientSession._read_chunked(reader)
169
+ elif "content-length" in headers:
170
+ body = await reader.readexactly(int(headers["content-length"]))
171
+ else:
172
+ body = await reader.read() # no framing: body ends when server closes
173
+ reusable = False
174
+
175
+ return Response(status, body, headers), reusable
176
+
177
+ @staticmethod
178
+ async def _read_chunked(reader) -> bytes:
179
+ chunks = []
180
+ while True:
181
+ size_line = await reader.readuntil(b"\r\n")
182
+ size = int(size_line.split(b";")[0].strip(), 16)
183
+ if size == 0:
184
+ while await reader.readuntil(b"\r\n") != b"\r\n":
185
+ pass # skip trailers
186
+ break
187
+ chunks.append(await reader.readexactly(size))
188
+ await reader.readexactly(2) # trailing CRLF after each chunk
189
+ return b"".join(chunks)
190
+
191
+ async def close(self):
192
+ self._closed = True
193
+ for pool in self._idle.values():
194
+ while pool:
195
+ _, writer = pool.pop()
196
+ writer.close()
@@ -0,0 +1,67 @@
1
+ """Request and Response objects shared by the client and the server."""
2
+ from __future__ import annotations
3
+
4
+ import json as _json
5
+ from dataclasses import dataclass, field
6
+
7
+ REASONS = {
8
+ 200: "OK",
9
+ 201: "Created",
10
+ 204: "No Content",
11
+ 301: "Moved Permanently",
12
+ 302: "Found",
13
+ 304: "Not Modified",
14
+ 400: "Bad Request",
15
+ 403: "Forbidden",
16
+ 404: "Not Found",
17
+ 405: "Method Not Allowed",
18
+ 413: "Payload Too Large",
19
+ 431: "Request Header Fields Too Large",
20
+ 500: "Internal Server Error",
21
+ 501: "Not Implemented",
22
+ 503: "Service Unavailable",
23
+ }
24
+
25
+
26
+ @dataclass
27
+ class Request:
28
+ method: str
29
+ path: str
30
+ query: str
31
+ version: str
32
+ headers: dict = field(default_factory=dict)
33
+ body: bytes = b""
34
+
35
+ def text(self) -> str:
36
+ return self.body.decode("utf-8")
37
+
38
+ def json(self):
39
+ return _json.loads(self.body)
40
+
41
+
42
+ class Response:
43
+ def __init__(self, status: int = 200, body=b"", headers: dict | None = None):
44
+ self.status = status
45
+ self.body = body.encode("utf-8") if isinstance(body, str) else bytes(body)
46
+ # Header names are stored lowercase so lookups are case-insensitive.
47
+ self.headers = {k.lower(): v for k, v in (headers or {}).items()}
48
+
49
+ @classmethod
50
+ def json(cls, data, status: int = 200) -> "Response":
51
+ return cls(
52
+ status,
53
+ _json.dumps(data).encode("utf-8"),
54
+ {"Content-Type": "application/json; charset=utf-8"},
55
+ )
56
+
57
+ def to_bytes(self, keep_alive: bool = True) -> bytes:
58
+ headers = dict(self.headers)
59
+ headers.setdefault("content-type", "text/plain; charset=utf-8")
60
+ headers["content-length"] = str(len(self.body))
61
+ headers["connection"] = "keep-alive" if keep_alive else "close"
62
+ head = [f"HTTP/1.1 {self.status} {REASONS.get(self.status, 'Unknown')}"]
63
+ head += [f"{k.title()}: {v}" for k, v in headers.items()]
64
+ return ("\r\n".join(head) + "\r\n\r\n").encode("latin-1") + self.body
65
+
66
+ def __repr__(self) -> str:
67
+ return f"<Response {self.status} ({len(self.body)} bytes)>"
@@ -0,0 +1,201 @@
1
+ """Asyncio HTTP/1.1 server with keep-alive, a connection cap, body limits and timeouts."""
2
+ from __future__ import annotations
3
+
4
+ import asyncio
5
+ import logging
6
+
7
+ from .response import Request, Response
8
+
9
+ log = logging.getLogger("mini_aiohttp.server")
10
+
11
+ MAX_LINE = 8 * 1024 # max size of one request or header line
12
+ MAX_HEADER_COUNT = 100
13
+
14
+
15
+ class BadRequest(Exception):
16
+ def __init__(self, status: int, message: str = ""):
17
+ super().__init__(message or str(status))
18
+ self.status = status
19
+
20
+
21
+ async def _readline(reader: asyncio.StreamReader) -> bytes:
22
+ try:
23
+ return await reader.readuntil(b"\r\n")
24
+ except asyncio.LimitOverrunError:
25
+ raise BadRequest(431, "Line too long")
26
+
27
+
28
+ def _check_fd_limit(needed: int) -> None:
29
+ """Warn early if the OS will refuse sockets before we hit max_connections."""
30
+ try:
31
+ import resource
32
+ except ImportError: # Windows
33
+ return
34
+ soft, _ = resource.getrlimit(resource.RLIMIT_NOFILE)
35
+ if soft != resource.RLIM_INFINITY and soft < needed:
36
+ log.warning(
37
+ "Open file limit is %d but about %d sockets are needed. "
38
+ "Raise it with `ulimit -n %d`.",
39
+ soft, needed, needed,
40
+ )
41
+
42
+
43
+ class Server:
44
+ def __init__(
45
+ self,
46
+ *,
47
+ max_connections: int = 100_000,
48
+ read_timeout: float = 15.0,
49
+ max_body_size: int = 10 * 1024 * 1024,
50
+ backlog: int = 4096,
51
+ ):
52
+ self.routes: dict = {}
53
+ self.max_connections = max_connections
54
+ self.read_timeout = read_timeout
55
+ self.max_body_size = max_body_size
56
+ self.backlog = backlog
57
+ self._active = 0
58
+ self._writers: set = set()
59
+ self._server = None
60
+
61
+ # ---------- routing ----------
62
+ def route(self, path: str, method: str = "GET"):
63
+ def decorator(handler):
64
+ self.routes[(method.upper(), path)] = handler
65
+ return handler
66
+ return decorator
67
+
68
+ async def _dispatch(self, req: Request) -> Response:
69
+ handler = self.routes.get((req.method, req.path))
70
+ if handler is None:
71
+ if any(path == req.path for _, path in self.routes):
72
+ return Response(405, "Method Not Allowed")
73
+ return Response(404, "Not Found")
74
+ try:
75
+ result = await handler(req)
76
+ except Exception:
77
+ log.exception("handler failed: %s %s", req.method, req.path)
78
+ return Response(500, "Internal Server Error")
79
+ return result if isinstance(result, Response) else Response(200, result)
80
+
81
+ # ---------- connection handling ----------
82
+ async def _on_connect(self, reader, writer):
83
+ try:
84
+ if self._active >= self.max_connections:
85
+ # Refuse fast instead of queueing: keeps memory bounded under overload.
86
+ await self._send(writer, Response(503, "Server busy"), keep_alive=False)
87
+ return
88
+ self._active += 1
89
+ self._writers.add(writer)
90
+ try:
91
+ await self._serve_connection(reader, writer)
92
+ finally:
93
+ self._active -= 1
94
+ self._writers.discard(writer)
95
+ except (ConnectionError, OSError, asyncio.IncompleteReadError):
96
+ pass # client went away mid-request; nothing to do
97
+ except Exception:
98
+ log.exception("unexpected error in connection handler")
99
+ finally:
100
+ writer.close()
101
+
102
+ async def _serve_connection(self, reader, writer):
103
+ while True:
104
+ try:
105
+ request = await asyncio.wait_for(self._read_request(reader), self.read_timeout)
106
+ except BadRequest as exc:
107
+ await self._send(writer, Response(exc.status, str(exc)), keep_alive=False)
108
+ return
109
+ except (asyncio.TimeoutError, asyncio.IncompleteReadError):
110
+ return # idle or slow client: drop the connection
111
+ if request is None:
112
+ return # clean EOF
113
+
114
+ keep_alive = self._wants_keep_alive(request)
115
+ response = await self._dispatch(request)
116
+ await self._send(writer, response, keep_alive)
117
+ if not keep_alive:
118
+ return
119
+
120
+ async def _read_request(self, reader) -> Request | None:
121
+ try:
122
+ line = await _readline(reader)
123
+ except asyncio.IncompleteReadError as exc:
124
+ if not exc.partial:
125
+ return None
126
+ raise
127
+
128
+ parts = line.decode("latin-1").rstrip("\r\n").split(" ")
129
+ if len(parts) != 3:
130
+ raise BadRequest(400, "Malformed request line")
131
+ method, target, version = parts
132
+ path, _, query = target.partition("?")
133
+
134
+ headers: dict = {}
135
+ while True:
136
+ raw = await _readline(reader)
137
+ if raw == b"\r\n":
138
+ break
139
+ if len(headers) >= MAX_HEADER_COUNT:
140
+ raise BadRequest(431, "Too many headers")
141
+ name, sep, value = raw.decode("latin-1").partition(":")
142
+ if not sep:
143
+ raise BadRequest(400, "Malformed header")
144
+ headers[name.strip().lower()] = value.strip()
145
+
146
+ if "chunked" in headers.get("transfer-encoding", "").lower():
147
+ raise BadRequest(501, "Chunked request bodies are not supported")
148
+ try:
149
+ length = int(headers.get("content-length", "0"))
150
+ except ValueError:
151
+ raise BadRequest(400, "Invalid Content-Length")
152
+ if length < 0:
153
+ raise BadRequest(400, "Invalid Content-Length")
154
+ if length > self.max_body_size:
155
+ raise BadRequest(413, "Request body too large")
156
+
157
+ body = await reader.readexactly(length) if length else b""
158
+ return Request(method.upper(), path, query, version, headers, body)
159
+
160
+ @staticmethod
161
+ def _wants_keep_alive(req: Request) -> bool:
162
+ conn = req.headers.get("connection", "").lower()
163
+ if req.version == "HTTP/1.1":
164
+ return "close" not in conn
165
+ return "keep-alive" in conn
166
+
167
+ @staticmethod
168
+ async def _send(writer, response: Response, keep_alive: bool) -> None:
169
+ writer.write(response.to_bytes(keep_alive))
170
+ await writer.drain()
171
+
172
+ # ---------- lifecycle ----------
173
+ async def start(self, host: str = "127.0.0.1", port: int = 8080, *, reuse_port: bool = False):
174
+ _check_fd_limit(self.max_connections + 64)
175
+ self._server = await asyncio.start_server(
176
+ self._on_connect,
177
+ host,
178
+ port,
179
+ backlog=self.backlog,
180
+ limit=MAX_LINE,
181
+ reuse_port=reuse_port,
182
+ )
183
+ return self._server
184
+
185
+ async def serve_forever(self, host: str = "127.0.0.1", port: int = 8080, **kwargs):
186
+ server = await self.start(host, port, **kwargs)
187
+ log.info("Listening on %s:%d", host, port)
188
+ async with server:
189
+ await server.serve_forever()
190
+
191
+ @property
192
+ def port(self) -> int:
193
+ return self._server.sockets[0].getsockname()[1]
194
+
195
+ async def close(self):
196
+ if self._server is not None:
197
+ self._server.close()
198
+ for writer in list(self._writers):
199
+ writer.close()
200
+ if self._server is not None:
201
+ await self._server.wait_closed()
@@ -0,0 +1,86 @@
1
+ Metadata-Version: 2.4
2
+ Name: mini-aiohttp
3
+ Version: 0.1.0
4
+ Summary: A small, dependency-free async HTTP/1.1 client and server built on asyncio
5
+ License: MIT
6
+ Requires-Python: >=3.9
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Provides-Extra: test
10
+ Requires-Dist: pytest>=7; extra == "test"
11
+ Requires-Dist: pytest-asyncio>=0.21; extra == "test"
12
+ Dynamic: license-file
13
+
14
+ # mini-aiohttp
15
+
16
+ A small, dependency-free async HTTP/1.1 client and server built on Python's `asyncio`.
17
+
18
+ ## Features
19
+
20
+ - Async client with keep-alive connection pooling per host
21
+ - Async server with routing, keep-alive, and request body limits
22
+ - Connection cap: new connections get `503` when the limit is reached, so memory stays bounded
23
+ - Timeouts on requests and idle connections
24
+ - Chunked response parsing (client) and `Content-Length` request bodies (server)
25
+ - HTTPS on the client side
26
+
27
+ ## Install
28
+
29
+ ```bash
30
+ pip install . # from source
31
+ pip install .[test] # with test dependencies
32
+ ```
33
+
34
+ ## Quick start
35
+
36
+ ```python
37
+ import asyncio
38
+ from mini_aiohttp import Server, Response, ClientSession
39
+
40
+ app = Server(max_connections=100_000)
41
+
42
+ @app.route("/")
43
+ async def index(req):
44
+ return Response(200, "Hello")
45
+
46
+ @app.route("/echo", method="POST")
47
+ async def echo(req):
48
+ return Response.json({"got": req.json()})
49
+
50
+ async def main():
51
+ server = asyncio.create_task(app.serve_forever("127.0.0.1", 8080))
52
+ await asyncio.sleep(0.2)
53
+ async with ClientSession() as s:
54
+ r = await s.get("http://127.0.0.1:8080/")
55
+ print(r.status, r.body.decode())
56
+ server.cancel()
57
+
58
+ asyncio.run(main())
59
+ ```
60
+
61
+ ## Running tests
62
+
63
+ ```bash
64
+ pip install .[test]
65
+ pytest
66
+ ```
67
+
68
+ ## Scaling to many connections
69
+
70
+ A single Python process can hold tens of thousands of idle keep-alive connections. Reaching 100k needs OS and deployment tuning:
71
+
72
+ 1. **File descriptor limit.** Each socket uses one. Raise it before starting: `ulimit -n 200000`. The server logs a warning if the limit is too low.
73
+ 2. **Listen backlog.** Raise the kernel queue: `sysctl -w net.core.somaxconn=65535`. The server already requests a backlog of 4096.
74
+ 3. **Client-side ports.** One client machine opening outbound connections to a single `ip:port` is limited by ephemeral ports (about 28k to 64k). For more, spread load across several source IPs or client hosts.
75
+ 4. **Multiple processes.** Python runs one event loop per process. Start several workers with `Server.start(..., reuse_port=True)` on Linux to use all CPU cores.
76
+ 5. **Measure it.** Test with a load tool such as `wrk`, `oha`, or `locust` on your own hardware before you rely on any number.
77
+
78
+ ## Limits (by design, v0.1)
79
+
80
+ - No HTTPS server (put a reverse proxy such as nginx in front).
81
+ - No chunked request bodies on the server.
82
+ - No HTTP/2 or WebSockets.
83
+
84
+ ## License
85
+
86
+ MIT
@@ -0,0 +1,14 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ mini_aiohttp/__init__.py
5
+ mini_aiohttp/client.py
6
+ mini_aiohttp/response.py
7
+ mini_aiohttp/server.py
8
+ mini_aiohttp.egg-info/PKG-INFO
9
+ mini_aiohttp.egg-info/SOURCES.txt
10
+ mini_aiohttp.egg-info/dependency_links.txt
11
+ mini_aiohttp.egg-info/requires.txt
12
+ mini_aiohttp.egg-info/top_level.txt
13
+ tests/test_client.py
14
+ tests/test_server.py
@@ -0,0 +1,4 @@
1
+
2
+ [test]
3
+ pytest>=7
4
+ pytest-asyncio>=0.21
@@ -0,0 +1 @@
1
+ mini_aiohttp
@@ -0,0 +1,22 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "mini-aiohttp"
7
+ version = "0.1.0"
8
+ description = "A small, dependency-free async HTTP/1.1 client and server built on asyncio"
9
+ readme = "README.md"
10
+ license = {text = "MIT"}
11
+ requires-python = ">=3.9"
12
+ dependencies = []
13
+
14
+ [project.optional-dependencies]
15
+ test = ["pytest>=7", "pytest-asyncio>=0.21"]
16
+
17
+ [tool.setuptools.packages.find]
18
+ include = ["mini_aiohttp*"]
19
+
20
+ [tool.pytest.ini_options]
21
+ asyncio_mode = "auto"
22
+ testpaths = ["tests"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,57 @@
1
+ import asyncio
2
+
3
+ import pytest
4
+
5
+ from mini_aiohttp import ClientError, ClientSession, Response, Server
6
+
7
+
8
+ @pytest.fixture
9
+ async def server():
10
+ srv = Server()
11
+
12
+ @srv.route("/")
13
+ async def index(req):
14
+ return Response(200, "ok")
15
+
16
+ await srv.start("127.0.0.1", 0)
17
+ yield srv
18
+ await srv.close()
19
+
20
+
21
+ async def test_keep_alive_reuses_connection(server):
22
+ async with ClientSession() as s:
23
+ await s.get(f"http://127.0.0.1:{server.port}/")
24
+ await s.get(f"http://127.0.0.1:{server.port}/")
25
+ assert len(s._idle[(False, "127.0.0.1", server.port)]) == 1
26
+
27
+
28
+ async def test_many_concurrent_requests(server):
29
+ url = f"http://127.0.0.1:{server.port}/"
30
+ async with ClientSession() as s:
31
+ results = await asyncio.gather(*(s.get(url) for _ in range(200)))
32
+ assert all(r.status == 200 for r in results)
33
+
34
+
35
+ async def test_reads_chunked_response():
36
+ async def handler(reader, writer):
37
+ await reader.readuntil(b"\r\n\r\n")
38
+ writer.write(
39
+ b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n"
40
+ b"5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n"
41
+ )
42
+ await writer.drain()
43
+ writer.close()
44
+
45
+ srv = await asyncio.start_server(handler, "127.0.0.1", 0)
46
+ port = srv.sockets[0].getsockname()[1]
47
+ async with srv:
48
+ async with ClientSession() as s:
49
+ r = await s.get(f"http://127.0.0.1:{port}/")
50
+ assert r.body == b"hello world"
51
+
52
+
53
+ async def test_closed_session_raises():
54
+ s = ClientSession()
55
+ await s.close()
56
+ with pytest.raises(ClientError):
57
+ await s.get("http://127.0.0.1:1/")
@@ -0,0 +1,66 @@
1
+ import pytest
2
+
3
+ from mini_aiohttp import ClientSession, Response, Server
4
+
5
+
6
+ @pytest.fixture
7
+ async def server():
8
+ srv = Server(read_timeout=5)
9
+
10
+ @srv.route("/")
11
+ async def index(req):
12
+ return Response(200, "hello")
13
+
14
+ @srv.route("/echo", method="POST")
15
+ async def echo(req):
16
+ return Response(200, req.body)
17
+
18
+ @srv.route("/boom")
19
+ async def boom(req):
20
+ raise RuntimeError("fail on purpose")
21
+
22
+ await srv.start("127.0.0.1", 0)
23
+ yield srv
24
+ await srv.close()
25
+
26
+
27
+ async def test_get_returns_200(server):
28
+ async with ClientSession() as s:
29
+ r = await s.get(f"http://127.0.0.1:{server.port}/")
30
+ assert r.status == 200
31
+ assert r.body == b"hello"
32
+
33
+
34
+ async def test_post_echoes_body(server):
35
+ async with ClientSession() as s:
36
+ r = await s.post(f"http://127.0.0.1:{server.port}/echo", data="ping")
37
+ assert r.body == b"ping"
38
+
39
+
40
+ async def test_unknown_path_is_404(server):
41
+ async with ClientSession() as s:
42
+ r = await s.get(f"http://127.0.0.1:{server.port}/missing")
43
+ assert r.status == 404
44
+
45
+
46
+ async def test_wrong_method_is_405(server):
47
+ async with ClientSession() as s:
48
+ r = await s.post(f"http://127.0.0.1:{server.port}/", data="x")
49
+ assert r.status == 405
50
+
51
+
52
+ async def test_handler_exception_is_500(server):
53
+ async with ClientSession() as s:
54
+ r = await s.get(f"http://127.0.0.1:{server.port}/boom")
55
+ assert r.status == 500
56
+
57
+
58
+ async def test_connection_cap_returns_503():
59
+ srv = Server(max_connections=0)
60
+ await srv.start("127.0.0.1", 0)
61
+ try:
62
+ async with ClientSession() as s:
63
+ r = await s.get(f"http://127.0.0.1:{srv.port}/")
64
+ assert r.status == 503
65
+ finally:
66
+ await srv.close()