-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This probably wont have well defined version but here is my first attempt here.
- Loading branch information
0 parents
commit 81a636b
Showing
6 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
ContentTypes = { | ||
"ico" : "image/webp", | ||
"png" : "image/webp", | ||
"jpg" : "image/webp", | ||
"html": "text/html;charset=utf-8", | ||
"txt" : "plain;charset=utf-8" | ||
} | ||
|
||
def get_ContentType(pageName): | ||
''' | ||
get the content type based on the name of a page | ||
''' | ||
if not "." in pageName: | ||
return "text/html" | ||
return ContentTypes[pageName.split('.')[1]] | ||
|
||
def send_Page(page,conn,ContentType="text/html"): | ||
''' | ||
page = page content | ||
conn = socket connection | ||
''' | ||
try: | ||
conn.send(f'HTTP/1.1 200 OK\nContent-Type: {ContentType}\nContent-Length: {len(page)}\nConnection: Closed\n\n'.encode()) | ||
conn.send(page) | ||
except: | ||
print("Connection Aborted: Transaction could not be completed") | ||
|
||
def send_404(conn): | ||
''' | ||
sends a 404 message. | ||
if PageSrc/404Page.html exists -> show page | ||
else -> show generic message | ||
''' | ||
try: | ||
with open('PageSrc/404Page.html','rb') as f: | ||
src = f.read() | ||
conn.send(f'HTTP/1.1 404 Not Found\nContent-Type: text/html;charset=utf-8\nContent-Length: {len(src)}\nConnection: Closed\n\n'.encode()) | ||
conn.send(src) | ||
except: | ||
ERROR_MSG = ''' | ||
<html> | ||
<head><title>404 PAGE NOT FOUND</title></head> | ||
<body><h1>404 PAGE NOT FOUND</h1></body> | ||
</html> | ||
''' | ||
conn.send(f'HTTP/1.1 404 Not Found\nContent-Type: text/html;charset=utf-8\nContent-Length: {len(ERROR_MSG)}\nConnection: Closed\n\n'.encode()) | ||
conn.send(ERROR_MSG.encode()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Handle_IO | ||
def handle_GET(request, conn): | ||
splitRequest = request.split(" ") | ||
with open("PageSrc"+splitRequest[1],"rb") as f: | ||
Handle_IO.send_Page(f.read(),conn, ContentType = Handle_IO.get_ContentType(splitRequest[1])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import socket | ||
import Handlers, Handle_IO | ||
HOST = "localhost" | ||
PORT = 1300 | ||
|
||
Functions = { | ||
"GET" : Handlers.handle_GET | ||
} | ||
|
||
Redirects = { | ||
"/" : "/index.html" | ||
} | ||
|
||
def changeTo_Redirect(text): | ||
splitText = text.split(" ") | ||
return splitText[0]+" "+Redirects[splitText[1]]+" "+splitText[2] | ||
|
||
|
||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
s.bind((HOST,PORT)) | ||
while True: | ||
s.listen() | ||
conn, addr = s.accept() | ||
with conn: | ||
try: | ||
data = conn.recv(1024).decode() | ||
dataSplit = data.split("\r\n") | ||
firstLine = dataSplit[0].split(" ") | ||
if not firstLine[2]=="HTTP/1.1": | ||
continue | ||
if firstLine[1] in Redirects.keys(): | ||
Functions[firstLine[0]](changeTo_Redirect(dataSplit[0]),conn) | ||
else: | ||
Functions[firstLine[0]](dataSplit[0],conn) | ||
except: | ||
Handle_IO.send_404(conn) | ||
|
||
s.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html5> | ||
<html> | ||
<head> | ||
<title>Home Page</title> | ||
|
||
</head> | ||
<body> | ||
<h1>Test Page</h1> | ||
<p>hi</p> | ||
<hr> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<html> | ||
<h1> A SECOND PAGE LOOSER MAN</h1> | ||
<a href="index.html">Test</a> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
![Version](https://img.shields.io/badge/Version-0.1-green) | ||
# Python WebServer | ||
|
||
This is a very basic webserver that can only handle `GET` requests as of now. | ||
|
||
## Suported Files | ||
|
||
|File Type|Into| | ||
|-|-| | ||
|.png|image/webp| | ||
|.ico|image/webp| | ||
|.jpg|image/webp| | ||
|.html|text/html;charset=utf-8| | ||
|.txt|plain;charset=utf-8| | ||
|
||
## How-to | ||
|
||
You must put all files in `PageSrc/`. Then run `Main.py` in your terminal. | ||
|
||
Lastly, go to whatever page you want by going to `localhost:1300/PAGENAME.EXTENSION` | ||
|
||
## Redirections | ||
|
||
Currently the only redirect redirects `/` -> `/index.html` but you can add as many as you want within the code. |