Skip to content

Commit

Permalink
Version 0.1 files added
Browse files Browse the repository at this point in the history
This probably wont have well defined version but here is my first attempt here.
  • Loading branch information
ArachnidAbby authored May 14, 2021
0 parents commit 81a636b
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Handle_IO.py
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())
5 changes: 5 additions & 0 deletions Handlers.py
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]))
38 changes: 38 additions & 0 deletions Main.py
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()
12 changes: 12 additions & 0 deletions PageSrc/index.html
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>
4 changes: 4 additions & 0 deletions PageSrc/test.html
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>
24 changes: 24 additions & 0 deletions README.md
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.

0 comments on commit 81a636b

Please sign in to comment.