This repository contains four Python scripts that demonstrate basic HTTP server and client interactions using both Flask and raw sockets. Below is an overview of each script and how they work.
This script creates a simple HTTP server using the Flask framework. The server listens on http://localhost:5000
and can handle both GET
and POST
requests at the /api
endpoint.
- GET Requests: The server extracts all query parameters from the URL and returns them in the response.
- POST Requests: The server extracts all form data sent in the request body and returns them in the response.
Usage:
python flask_api.py
After running the script, you can test it using a browser or a tool like curl
.
This script demonstrates how to send a GET
request to the flask_api.py
server using raw sockets.
- The request is manually constructed, and the parameters are included in the URL.
- The script sends the request and prints the server’s response.
Usage:
python get.py
Make sure the flask_api.py
server is running before executing this script.
This script demonstrates how to send a POST
request to the flask_api.py
server using raw sockets.
- The request is manually constructed, including both headers and a body containing form data.
- The script sends the request and prints the server’s response.
Usage:
python post.py
Make sure the flask_api.py
server is running before executing this script.
This script implements a simple HTTP server using only Python’s built-in socket
library. The server can handle both GET
and POST
requests at the /api
endpoint.
- Routing: A decorator (
@route
) is used to register handler functions for specific paths and methods. - Request Parsing: The script manually parses the HTTP request, extracting the method, path, headers, and body.
- Dynamic Responses: Based on the request method (
GET
orPOST
), the server responds with the received parameters.
Usage:
python socket_api.py
After running the script, you can test it using get.py
and post.py
scripts or by sending requests manually.
- GET vs. POST:
GET
requests send data as part of the URL (query parameters), whereasPOST
requests send data in the request body. - Raw Sockets: The
get.py
andpost.py
scripts illustrate how HTTP requests can be sent using raw sockets, giving you control over the entire HTTP request structure. - Flask vs. Raw Sockets:
flask_api.py
is a high-level abstraction using the Flask framework, whereassocket_api.py
demonstrates a low-level approach to building an HTTP server from scratch.
-
Start the Flask Server:
python flask_api.py
-
Send a GET Request:
python get.py
-
Send a POST Request:
python post.py
-
Start the Raw Socket Server:
python socket_api.py
-
Test the Raw Socket Server: Use the
get.py
andpost.py
scripts to send requests tosocket_api.py
or use a tool likecurl
to send requests manually.
These scripts are educational examples showing how HTTP requests and responses work at both high and low levels of abstraction. They are useful for understanding the basics of HTTP communication, the difference between GET
and POST
methods, and how to build simple HTTP servers and clients in Python.