Skip to content

Commit 3e18480

Browse files
committed
Created a simple HTTP server project
1 parent 48fb214 commit 3e18480

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

Simple_Http_Server/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!--Please do not remove this part-->
2+
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
3+
![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)
4+
5+
# Simple HTTP Server
6+
7+
Add a jpeg/png/gif file here if applicable
8+
9+
<!--An image is an illustration for your project, the tip here is using your sense of humour as much as you can :D
10+
11+
You can copy paste my markdown photo insert as following:
12+
<p align="center">
13+
<img src="your-source-is-here" width=40% height=40%>
14+
-->
15+
16+
## 🛠️ Description
17+
A simple HTTP server written using python sockets.
18+
19+
## ⚙️ Languages or Frameworks Used
20+
This project is written in Python and has no other dependencies other than the Python Standard library.
21+
22+
## 🌟 How to run
23+
<!--Remove the below lines and add yours -->
24+
Steps on how to run the script along with suitable examples.
25+
26+
## 📺 Demo
27+
Add a Screenshot/GIF showing the sample use of the script (jpeg/png/gif).
28+
29+
## 🤖 Author
30+
[Harish Kumar](https://github.com/harishtpj)

Simple_Http_Server/mhttp.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# mHTTP - A simple HTTP server
2+
# Written by M.V.Harish Kumar on 24/10/2023
3+
4+
import sys, socket
5+
from pathlib import Path
6+
7+
HOST = "0.0.0.0"
8+
PORT = 1997
9+
FOLDER = '.' if len(sys.argv) < 2 else sys.argv[1]
10+
11+
def get_content(path):
12+
ext = "html"
13+
match path:
14+
case "/":
15+
try:
16+
with open(FOLDER + "/index.html", "r") as f:
17+
content = f.read()
18+
except FileNotFoundError:
19+
content = "The Server is working! but there is no index.html file to render"
20+
case _:
21+
try:
22+
with open(FOLDER + path, "r") as f:
23+
if Path(FOLDER + path).suffix != ".html":
24+
ext = "plain"
25+
content = f.read()
26+
except FileNotFoundError:
27+
return 404, "File not found", ext
28+
return 200, content, ext
29+
30+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
31+
try:
32+
s.bind((HOST, PORT))
33+
s.listen()
34+
35+
print("mHTTP: The Micro-HTTP Server")
36+
print(f"Server Started running at {HOST}:{PORT}\n")
37+
print("mhttp: waiting for connections...")
38+
39+
while True:
40+
clnt, caddr = s.accept()
41+
with clnt:
42+
print(f"mhttp: got connection from {caddr[0]}:{caddr[1]}")
43+
req = clnt.recv(1024).decode()
44+
if not req:
45+
print("mhttp: connection closed unexpectedly", file=sys.stderr)
46+
break
47+
48+
req = req.split("\r\n")
49+
print(f"mhttp: got request: {req[0]}")
50+
path = req[0].split(" ")[1]
51+
52+
sts_cd, content, ftype = get_content(path)
53+
54+
resp = f"HTTP/1.1 {sts_cd}\r\n" \
55+
f"Content-Type: text/{ftype}\r\n" \
56+
"\r\n" + content
57+
58+
clnt.sendall(resp.encode())
59+
print(f"mhttp: sent response({sts_cd}) to {caddr[0]}:{caddr[1]}")
60+
except KeyboardInterrupt:
61+
print("mhttp: Got Keyboard Interrupt", file=sys.stderr)
62+
print("mhttp: Closing Connection.", file=sys.stderr)
63+
64+

0 commit comments

Comments
 (0)