Git Product home page Git Product logo

ref1o / http-server Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 7 KB

This project is a simple HTTP server implemented in Python. It handles various types of HTTP requests, including GET and POST, and supports functionalities like echoing request data, serving files, handling user-agent requests, and more. The server also supports gzip compression for responses.

License: MIT License

Python 99.12% Shell 0.88%

http-server's Introduction

HTTP-server

Description

This project is a simple HTTP server implemented in Python. It handles various types of HTTP requests, including GET and POST, and supports functionalities like echoing request data, serving files, handling user-agent requests, and more. The server also supports gzip compression for responses.

Directory Structure

.
├── app
│   └── main.py
└── run_server.sh

Files

main.py

The main script of the HTTP server located in app/main.py. It includes the following functionalities:

  • Handles incoming HTTP requests and parses them.
  • Supports endpoints for echoing, blank responses, user-agent, serving files, and creating files.
  • Supports gzip compression for responses if requested by the client.
  • Multithreaded handling of client connections.

run_server.sh

A shell script to run the HTTP server using pipenv.

Endpoints

  • /: Returns a blank response.
  • /echo/{message}: Echoes the {message} part of the URL.
  • /user-agent: Returns the user-agent header sent by the client.
  • /files/{filename}: Serves the file {filename} if it exists in the specified directory.
  • POST /files/{filename}: Creates a new file {filename} with the body of the request as its content.

Setup and Usage

Prerequisites

  • Python 3.x
  • pipenv

Installation

  1. Clone the repository:
git clone https://github.com/ref1o/HTTP-server.git
cd HTTP-server   
  1. Install dependencies:
pipenv install  

Running the Server

  1. Run the server using the provided shell script:
./run_server.sh /path/to/files/directory  

The server will start on localhost at port 4221.

Example Requests

  • To get a blank response:
curl http://localhost:4221/
  • To escho a message:
curl http://localhost:4221/echo/hello
  • To get the user-agent:
curl http://localhost:4221/user-agent
  • To retrieve a file:
curl http://localhost:4221/files/filename.txt
  • To create a new file:
curl -X POST -d "file content" http://localhost:4221/files/filename.txt

Code Overview

Imports and Costants

The script imports various modules and sets up some constants:

  • socket, os, sys, pprint, asyncio, gzip, concurrent.futures.ThreadPoolExecutor, collections.defaultdict
  • Constants: CRLF, ENCODING, statuses

Functions

  • get_request(sock): Reads and parses the incoming HTTP request.
  • check_encoding(request_headers, response_headers): Checks and validates encoding from the request headers.
  • compress(type, data): Compresses the response data if needed.
  • response(status, headers): Constructs an HTTP response.
  • end_point_echo(sock, request): Handles the echo endpoint.
  • end_point_blank(sock): Handles the root endpoint returning a blank response.
  • end_point_path(sock, request): Handles requests to non-existent paths.
  • end_point_user_agent(sock, request): Handles the user-agent endpoint.
  • end_point_file(sock, request): Serves files from the specified directory.
  • create_file(sock, request): Creates a file with the content from the request body.
  • handler(c_sock, addr): Main handler for incoming client connections.
  • main(): Main function to set up the server and handle connections using threads.

License

This project is licensed under the MIT License.

http-server's People

Contributors

ref1o 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.