Giter VIP home page Giter VIP logo

http_server's Introduction

HTTP Server

This is a simple HTTP server implemented in Python using sockets. It provides a basic framework for handling GET and POST requests with custom routes and serving static files.

Getting Started

To use this HTTP server, follow these steps:

  1. Import the HTTPServer class from modules.HTTPServer
from modules.HTTPServer import HTTPServer
  1. Create a new instance of the HTTPServer class
server = HTTPServer('localhost', 8080)
  1. Create callback functions for each route you want to handle
def index(request, response):
    response.set_body(b'Hello, World!')
    response.send()

def about(request, response):
    response.set_body(b'About page')
    response.send()

def forbidden(request, response):
    response.status(403).send(custom = b'Forbidden')
  1. Add the routes to the server
server.get('/',callback=index)
server.get('/about',callback=about)
server.get('/forbidden',callback=forbidden)
  1. Start the server
server.run()

Once the server is running, navigate to http://localhost:8080 in your web browser to access the custom route for GET requests. For POST requests, you can use appropriate tools like curl or HTML forms.

Features

1. Request Handling

The server can handle incoming client requests. It parses the request and creates a corresponding Request object.

2. Response Handling

The server creates a Response object to construct the response and sends it back to the client.

3. Routing

You can add custom routes for GET and POST requests using the get() and post() methods, respectively.

4. Serving Static Files

The server is capable of serving static files located in the specified root directory.

5. Logging

The server logs incoming requests and responses to a file specified during initialization.

Example

from http_server import HTTPServer

def handle_get_request(request, response):
    response.status(200).send(b"Hello, World!")
    

def handle_post_request(request, response):
    # Custom logic to handle POST requests
    # ...
    pass

if __name__ == "__main__":
    server = HTTPServer('localhost', 8080)
    server.get('/', handle_get_request)
    server.post('/submit', handle_post_request)
    server.get('/static', root_dir = 'static')
    server.run()

License

This project is licensed under the MIT License.

http_server's People

Contributors

raidenblackout avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.