https://github.com/ahmethakanbesel/http-socket
A socket level HTTP server and client implementation in Python. Only GET and POST methods are implemented. Educational purposes only, not suitable for production.
https://github.com/ahmethakanbesel/http-socket
http http-client http-server multithreading socket socket-programming
Last synced: 11 months ago
JSON representation
A socket level HTTP server and client implementation in Python. Only GET and POST methods are implemented. Educational purposes only, not suitable for production.
- Host: GitHub
- URL: https://github.com/ahmethakanbesel/http-socket
- Owner: ahmethakanbesel
- Created: 2023-02-05T08:42:50.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-05T08:47:42.000Z (about 3 years ago)
- Last Synced: 2025-01-30T09:24:59.941Z (about 1 year ago)
- Topics: http, http-client, http-server, multithreading, socket, socket-programming
- Language: Python
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Multi-threaded HTTP Socket Server and HTTP Client
A socket level HTTP server and client implementation in Python.
Only GET and POST methods are implemented. Educational purposes only, not suitable for production.
## HTTP Server Usage
### Create a handler to handle incoming requests
```python
from http_server import BaseHandler
from templates import get_template
class IndexHandler(BaseHandler):
def get(self):
self.response = get_template('base').format('Homepage', 'Homepage',
"
Method: GET
Query Params
{}".format(
self.request['params']))
pass
def post(self):
self.response = get_template('base').format('Homepage', 'Homepage',
"
Method: POST
Post Fields
{}".format(
self.request['form']))
pass
```
### Create a multi-threaded HTTP server and define routes
```python
from http_server import HTTPServer
from handlers import IndexHandler
example_server = HTTPServer('localhost', 8000, 5, {
"/": IndexHandler,
})
example_server.start()
```
## HTTP Client Usage
```python
import socket
from http_client.client import get_status_code
from http_client import client, parse_response
# GET request
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
response = client.request('GET', 'localhost', 8000, '/index',
sock)
response = parse_response(response)
# Retrieve only status code
status_code = get_status_code('GET', 'localhost', 8000, '/index')
```
## Benchmarking
A simple configuration for Locust can be found in `locustfile.py`.
If you have not installed Locust yet you can install it by
```bash
pip install locust
```
Then run `locust` command in your terminal to start Locust.