This is a simple WSGI (Web Server Gateway Interface) framework called Jahan. It provides a basic structure for building web applications in Python. It includes a Request object, a Response object, and a Router object for handling incoming HTTP requests and generating appropriate responses.
This repository also contains WSGIserver
, which can be used to serve WSGI application builtin in Jahan
Further documentation can be found Here
-
Create an instance of the
Jahan
class and define your web application routes using theadd_route
method. -
Define callback functions for each route. Each callback function should take a
Request
object as its first argument. -
The callback function should process the request, generate a response (either a
Response
object or aTemplateResponse
object), and return it. -
Run the application using the
run()
method of theJahan
class. -
The application will start serving at
http://127.0.0.1:8000/
. You can access the application in your web browser.
Demo application: first clone the repository containing Jahan Framework
git clone https://github.com/faishal882/jahan-demo-app.git
It already has demo application built, After cloning the file structure must be in
demo_jahan_app/
/app
__init__.py
src.py
models.py
/jahan (this folder contains the Framework)
__init__.py
/tests
tests.py
jahan.py
/templates
add_employee.html
employees.html
main.py
...
Please create and start and virtual enviornment before running the application
python -m venv <env-name>
In windows activate virtual enviornment
<env-name>/Scripts/activate.bat
Install pre-run requirements
pip install -r requirements.txt
To run the application locally with wsgiref server
python main.py
The application will start serving at http://127.0.0.1:8000/
. You can access the application in your web browser.
- paste this code in main.py, after importing WSGIServer class from server module
if __name__ == '__main__':
try:
server = WSGIServer('localhost', 8000, application)
server.serve_forever()
except KeyboardInterrupt:
print("exit")
- run
python main.py
The application will start serving at http://127.0.0.1:8000/
. You can access the application in your web browser.
-
Request
class: A wrapper for WSGI environment dictionaries. It provides read-only access to various properties of the HTTP request, such as query string parameters, request method, path, and WSGI environment. -
Response
class: A class for creating HTTP responses. It allows you to set the response body, status code, content type, and other response headers. -
TemplateResponse
class: An inherited class fromResponse
. It allows you to render a Jinja2 template and use it as a response. -
Router
class: A class for storing URL routes and their corresponding callbacks. It provides methods to add new routes and match a URL path to a specific callback. -
Jahan
class: The main application class that represents a single web application. It includes a router to handle URL routing. It is a callable WSGI application.
-
Request
class:get_qs
: Returns a dictionary of query string parameters provided by the user.get_post
: Returns a dictionary of data provided by the user via POST method.path
: Returns the path of the request.method
: Returns the method of the request (e.g., GET, POST, etc.).env
: Returns the whole WSGI environment passed in the request.
-
Response
class:status
: Returns the HTTP status code and reason phrase of the response.__iter__()
: Iterates over the response body and yields data to be sent to the client.
-
TemplateResponse
class:__iter__()
: Renders the Jinja2 template and yields the response body. Jinja2 templating engine is implemented, so context can be simply accessed in html using {{ context }}. For further documentation please visit offical documentation site of Jinja.
TemplateResponse("template.html", context={"Name": "John"})
In html file
<p>{{ Name }}</p>
-
Router
class:add_route(pattern, callback)
: Adds a new URL route with a corresponding callback function.match(path)
: Matches a URL path with a callback function from the routing table.
-
Jahan
class:run()
: Starts the WSGI application with the built-in python wsgiref server.add_route(route)
: Adds a new route object to the router.The argument given should be regex. This is decorator method, Use:
app = Jahan(__name__) @app.add_route(r"/$") def hello_world(request): pass
In above code hello_world is added to url route
<your-domain-name>/
as callback function
-
The
Jahan
class creates an instance of theRouter
class to store URL routes and their callbacks. -
The user defines URL routes using the
add_route
decorator method of theJahan
class and provides corresponding callback functions. -
When an HTTP request is received, the
application
method of theJahan
class is called. -
The
Router
class matches the URL path of the request with a callback function from the routing table. Routing table stores all the url routes with its callback function -
The matched callback function is called with a
Request
object as its argument. -
The callback function processes the request, generates a response, and returns it as a
Response
object. -
The
application
method sets the response status and headers, and iterates over the response body to send it to the client. -
The WSGI server starts serving the application, and it continues to serve incoming requests until it is terminated.
The framework uses the following Python standard libraries and third-party modules:
re
: For regular expressions.urllib
: For parsing query string and post data.http
module: For handling HTTP status codes and reason phrases.wsgiref.headers
: For handling response headers.wsgiref.simple_server
: For creating a simple WSGI server.jinja2.Template
: For rendering Jinja2 templates.
The WSGIServer
class is a simple WSGI server that implements the Python WSGI protocol for running web applications on a local machine. It is used to serve WSGI applications like the Jahan
class defined in the Jahan framework.
host
: str, the host IP address or domain name where the server will listen for incoming connections.port
: int, the port number on which the server will listen for incoming connections.app
: WSGI application, a callable object conforming to the WSGI interface.
-
__init__(self, host: str, port: int, app)
- Initialize the
WSGIServer
instance with the given host, port, and WSGI application.
- Initialize the
-
handle_request(self, client_socket)
- Handle an incoming client request by parsing the request, invoking the provided WSGI application, and sending back the response to the client.
- Parameters:
client_socket
: socket, the client socket representing the incoming connection.
-
start_response(self, status, response_headers)
- Callback function for the WSGI application to set the response status and headers.
- Parameters:
status
: str, the HTTP status code and message (e.g., '200 OK').response_headers
: list of tuples, containing the response headers (e.g., [('Content-Type', 'text/html')]).
-
send_response(self, client_socket, response)
- Send the HTTP response to the client.
- This method takes the response generated by the WSGI application and sends it back to the client through the provided client socket. The response includes the HTTP status line, headers, and the response body.
- Parameters:
client_socket
: socket, the client socket representing the incoming connection.response
: WSGI response, the response generated by the WSGI application.
-
serve_forever(self)
- Start the WSGI server and listen for incoming connections indefinitely until interrupted.
- This method binds the server socket to the provided host and port, starts listening for incoming connections, and handles each connection in a separate thread.
- The server will keep running until a KeyboardInterrupt (Ctrl+C) is received, at which point it will close the server socket and stop listening for new connections.
The WSGIServer
class relies on the socket
, threading
, and io
modules from the Python standard library.
1). Create an instance of the WSGIServer
class and provide the Jahan
application as the WSGI application.
server = WSGIServer('127.0.0.1', 8000, Jahan('MyApp').application)
2). Start the server and listen for incoming connections.
server.serve_forever()
3). The server will run on http://127.0.0.1:8000/ and will serve the Jahan
web application.
The WSGIServer
implementation may not cover all security issues, so it's best suited for local development and testing purposes.