Skip to main content

A simple Python library to build web applications.

Project description

amweb

A lightweight and complete Python library for building simple web servers.

Features Integrated Web Structure: Includes classes for Request, Response, Router, and Server.

Request Routing: Automatically directs each URL to a specific function using the Router class.

Easy Header Building: Build complex HTTP headers in a single line.

Automatic Request Parsing: Parses incoming requests (GET and POST) automatically.

Standard Status Codes: Supports standard HTTP status codes and their messages.

Installation You can install the library easily using pip once it's published on PyPI:

Bash

pip install amweb How to Use amweb This library is designed to make it simple to build a web server from scratch. All you need is a single Python file to run your server.

Step 1: Create Your Server First, create a Server object. This class handles the low-level socket connections and listens for incoming requests.

Python

main_server.py

from amweb.server import Server

HOST = '0.0.0.0' PORT = 8080

Create your server instance

app = Server(HOST, PORT)

Start the server

if name == "main": app.run() Step 2: Define Your Routes Use the @app.route decorator to connect specific URLs to a function. The decorator takes two arguments: the path (e.g., "/", "/about") and the HTTP method (e.g., "GET", "POST").

Python

from amweb.response import Response

@app.route("/", "GET") def index_page(client_connection, request): html_content = "

Welcome to amweb!

" # The handler function takes two arguments: # 1. client_connection: for sending the response # 2. request: the parsed request object

# Use Response to build and send the final data
response = Response(200, "text/html", html_content)
client_connection.sendall(response.build())

@app.route("/contact", "GET") def contact_page(client_connection, request): html_content = "Contact us at my_email@example.com" response = Response(200, "text/html", html_content) client_connection.sendall(response.build()) Step 3: Handle Requests The request object provides easy access to all the information from the browser. You can access headers, the request method, and the path.

Python

@app.route("/user-info", "GET") def user_info(client_connection, request): # Access a header (e.g., User-Agent) user_agent = request.headers.get("User-Agent", "Not specified")

# Access the request path
path = request.path

# Access the request method (GET, POST)
method = request.method

# You can also get POST data from request.body
# ...

Step 4: Build a Response Use the Response class to build the final HTTP response. It handles everything for you and is a much better way than manually writing the headers.

Python

from amweb.response import Response

This creates a complete HTTP response with headers and body

my_response = Response( 200, # Status Code (e.g., 200) "text/html", # Content Type "

Success!

" # Content )

Use the .build() method to get the final bytes to send

final_bytes = my_response.build() License This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

amweb-0.0.5.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

amweb-0.0.5-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file amweb-0.0.5.tar.gz.

File metadata

  • Download URL: amweb-0.0.5.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for amweb-0.0.5.tar.gz
Algorithm Hash digest
SHA256 a5db5a9e9ba34f2ff89bd8f22f93cf08a8daa1db0fb3af8867b49ee874a3c1b1
MD5 688ebf23c69e164ce106cb45e3c44195
BLAKE2b-256 f95c38f9aa18529423de5e7b69d986f66add00861d6b21a725c1dc736bdf175f

See more details on using hashes here.

File details

Details for the file amweb-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: amweb-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for amweb-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 0c4bb2c6fa5691e9188d9cc14e22d40be90427f7513fad04f7f4f2dad1cbfeb2
MD5 7d977d5354ff532a0a4353b822a670e7
BLAKE2b-256 225005091bc016340a5b8c02cf4b819e11fa2afb7006ecba5b21734c5e61724a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page