---
# System prepended metadata

title: Meow HTTP Server

---

# Meow HTTP Server

[TOC]


## Basic Architecture 

### Server.py 

```python
import socket 
from router import Router

class Banner:

    HEADER = '\033[5m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    CHERRY_BLOSSOM = '\033[1;35m'

    def __init__(self):
        self.meowBanner()

    def meowBanner(self):
        
        content = "Welcome, MeowHttpServer. ^O^"
        contentColor = self.ENDC + self.CHERRY_BLOSSOM + content + self.ENDC + self.HEADER
        countLength = len(content)
        if countLength % 2 != 0:content = content + " "
        countLenth2 = countLength // 2
        spaceNum = 24 - countLenth2
        # print("Number of characters:", countLength)
        
        print(self.HEADER)
        print("#"*50)
        print("#"+" "*48+"#")
        print("#" + " "*spaceNum + contentColor+ " "*spaceNum + "#")
        print("#"+" "*48+"#")
        print("#"*50)
        print(self.ENDC)
        
        print(self.WARNING + "-> Author: Meowhecker\n" + self.ENDC)

def server():

    serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1) # Once Deamon stop -> release socket Port
    serverSocket.bind(('0.0.0.0', 80))
    serverSocket.listen(20)

    print("Server Start http://127.0.0.1:80")

    while 1:

        # Receive Connection From Client !
        clientSocket, addr = serverSocket.accept()
        print("Receive Connection from ", addr)
        
        # Handle Cleint request
        requestHandler(clientSocket)


    serverSocket.close()


def requestHandler(clientSocket):
    
    router = Router()

    #Receive Data from Client 
    clientTcpPayload = clientSocket.recv(1024).decode()
    clientRequestLines = clientTcpPayload.split('\r\n')

    #Extract Frist Line in HTTP Request
    clientRequestFristLine = clientRequestLines[0]
    print("Request Path = ",clientRequestFristLine)

    #Assign Method / PATH / Version 
    clientRequestMethod = clientRequestFristLine.split(' ')[0]
    clientRequestPath = clientRequestFristLine.split(' ')[1]
    clientRequestVersion = clientRequestFristLine.split(' ')[2]

    #router
    serverResponse = router.route(clientRequestPath)
    
    clientSocket.sendall(serverResponse.encode())
    clientSocket.close()

banner=Banner()
server()
```


### Router.py


```python

#router.py
import os
from view import View


# Current Path
currentDirPath = os.path.dirname(os.path.abspath(__file__))
print(currentDirPath)

class Router():

    def __init__(self):
        
        self.data = {
            "name": "meowhecker",
            "email": "meowhecker@meow.com"
        }
        self.currentDirPath = os.path.dirname(os.path.abspath(__file__))
        

    def route(self,reqPath):
        if reqPath == "/":
            templatePath = os.path.join(self.currentDirPath,"views","index.html")
            return View.render(templatePath,self.data)
        else:
            return self.notFound()

    def notFound(self):
        return "<h1>404 Not Found  ~ Meow</h1>"
```

### View.py

```python
#view.py

class View:

    @staticmethod  # Method, attribute allow directly invoke Without creating  Instance  
    def render(templatePath, data=None): 
        print(data)
        renderContent = ""
        with open(templatePath, 'r') as template:

            templateContent = template.read() # (Strings) 

            if data :
                renderContent = templateContent.format(**data) # Template {name} -> name
            else :
                renderContent = templateContent

        return renderContent
```