Skip to main content

NetJin is a lightweight web server implemented in Python using sockets. It provides a simple implementation of the HTTP protocol, allowing users to build and run web applications with ease.

Project description

NetJin - Simple Python Web Server Implementation

Overview

NetJin is a lightweight web server implemented in Python using sockets. It provides a simple implementation of the HTTP protocol, allowing users to build and run web applications with ease.

Features

  • Routing: Easily define routes for different URLs and HTTP methods.
  • Request Handling: Handle incoming HTTP requests and process them accordingly.
  • Response Rendering: Render HTML templates for generating dynamic content and return other responses as well.
  • Error Handling: Customizable error handling for managing HTTP errors.

Installation

You can install NetJin using pip:

pip install WebServer

Alternatively, you can clone or download the repository and include the NetJin file in your project directory.

Usage

  1. Import the necessary classes:

    from WebServer import WebServer, Request, Response
    
  2. Create an instance of the WebServer class:

    app = WebServer(debug=True)
    
  3. Define routes using the @app.route() decorator, specifying the URL path and supported HTTP methods. For example:

    @app.route("/", methods=["GET"])
    def home(request: Request, response: response) -> None:
        return response.render("index") # This corresponse to index.html
    
  4. Implement functions to handle each route, receiving Response and Request objects as parameters.

  5. Run the server:

    if __name__ == "__main__":
        app.run()
    

Settings

To configure your project, create a file named Settings.py in a directory named Configurations at the root of your project. Define the required variables as needed. For example:

from pathlib import Path
from typing import List

__all__ = [
    "BASE_DIR",

    "HOST",
    "PORT",
    "DEBUG",
    "ALLOWED_HOST",

    "TEMPLATE_DIRS",
    "STATIC_DIRS",
    "PUBLIC_DIR",
    "MEDIA_DIR",
]

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent

# Turn off (False) for production.
DEBUG: bool = True

# Server Configuration
HOST: str = "127.0.0.1"
PORT: int = 8000
ALLOWED_HOST: List[str] = []

# Path to HTML files
TEMPLATE_DIRS: str | List[str] = "templates"  # BASE_DIR / "templates"
# Path to static folders (Directory for CSS, JS, Image, etc.)
STATIC_DIRS: List[str] = ["statics"]  # BASE_DIR / "statics"
# Path for public directory (where non-static files are located.)
PUBLIC_DIR: str = "public"
# Path where media files are stored.
MEDIA_DIR: str = "media"

Examples

Check out the example directory for sample usage and demonstrations.

Dependencies

  • No external dependencies. Uses only Python's built-in socket module.

Contribution

Contributions are welcome! Feel free to fork the repository, make improvements, and submit pull requests.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

For any inquiries or support, please contact acharyaraj.webserver@gmail.com or visit the GitHub profile.

.
├── config/
│   ├── settings.py
│   ├── __init__.py
│   └── other_app_configurations.py
├── static/
│   ├── csses
│   └── javascripts
├── media/
│   ├── images
│   └── videos
├── public/
│   ├── logo
│   ├── robot.txt
│   └── manifest.json
├── views/
│   ├── index.html
│   └── Other HTMLs
└── main.py

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

netjin-1.1.1.tar.gz (479.1 kB view details)

Uploaded Source

Built Distribution

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

netjin-1.1.1-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file netjin-1.1.1.tar.gz.

File metadata

  • Download URL: netjin-1.1.1.tar.gz
  • Upload date:
  • Size: 479.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.30

File hashes

Hashes for netjin-1.1.1.tar.gz
Algorithm Hash digest
SHA256 68d24ba52417ea7e41e98bc7e0b9930bb6bb0762af4219c5a07bbc738156a5e7
MD5 d2caf1d99f71c62dad7b9625cf3910ab
BLAKE2b-256 aa989c42f50e7e79bd6c95559bd0461bac84499b8206908e0b820ea8600eaafd

See more details on using hashes here.

File details

Details for the file netjin-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: netjin-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.30

File hashes

Hashes for netjin-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c8329ce4750460d6a7a2d0669336f17ce47176734fe07fcadc3b8293ba5000fa
MD5 c27e5131b4ed334444dc3d2731d4f650
BLAKE2b-256 a1598fce5af09dedf30c160b37c6083955d99b0602bf1c3356f9dc099d3d2f80

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