flowno.io.http_server
Simple HTTP server implementation for Flowno applications.
This module provides a basic HTTP server that works with Flowno’s event loop. It’s intended for testing, development, and simple use cases, not for production use.
Example
Basic HTTP server:
>>> from flowno import EventLoop
>>> from flowno.io import HttpServer
>>>
>>> async def main():
... server = HttpServer('localhost', 8080)
... await server.serve() # This will run forever, handling requests
...
>>> # Run in a separate thread or process since it blocks indefinitely
>>> loop = EventLoop()
>>> loop.run_until_complete(main())
- class flowno.io.http_server.HttpServer(host: str, port: int)[source]
Simple HTTP server compatible with the Flowno event loop.
This is a minimal HTTP server implementation for development and testing. For production scenarios, it’s recommended to use a dedicated web server like Flask or FastAPI in a separate process.
- host
The hostname or IP address to bind to
- port
The port number to listen on
Example
>>> from flowno import EventLoop >>> from flowno.io import HttpServer >>> >>> async def custom_server(): ... server = HttpServer('localhost', 8080) ... await server.serve() ... >>> loop = EventLoop() >>> loop.run_until_complete(custom_server())
- async _generate_response(request_data: str) bytes [source]
Generate a response based on the request.
This is a simple implementation that returns a generic response. Override this method in a subclass to provide custom response logic.
- Parameters:
request_data – The first line of the HTTP request (e.g., “GET / HTTP/1.1”)
- Returns:
Response body as bytes
- async _receive_all(sock: SocketHandle) bytes [source]
Receive all data from a socket until connection closes.
- Parameters:
sock – The socket to read from
- Returns:
All data received from the socket
- async _receive_headers(sock: SocketHandle) tuple[str, Headers] [source]
Receive and parse HTTP headers from a client connection.
This method reads from the socket until it encounters the end of headers marker (double CRLF), then parses the headers.
- Parameters:
sock – The socket to read from
- Returns:
Tuple of (request_line, headers)
- async handle_client(client_sock: SocketHandle)[source]
Handle an individual client connection.
This method reads the client request, processes it, and sends a response.
- Parameters:
client_sock – The socket connected to the client
- async serve()[source]
Start the HTTP server and begin accepting connections.
This method binds to the specified host and port, then enters an infinite loop to accept and handle client connections. Each client connection is handled in a separate task.
The current task will suspend and other tasks can run concurrently.