Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import threading
- import routing
- class Parvus(object):
- def __init__(self, hostname, port=80):
- self.http = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.host = (hostname, port)
- self.routes = dict()
- self.thread = threading.Thread(None, self)
- def __call__(self):
- while True:
- client,address = self.http.accept()
- threading.Thread(None, routing.handle_request, None, (client,address,self.routes)).start()
- def add_route(self, paths, output, **kwargs):
- content_type = kwargs.pop("content_type", "text/plain")
- for method in kwargs.pop("methods", ("GET",)):
- method = method.upper()
- if method not in self.routes:
- self.routes[method] = list()
- self.routes[method].append(routing.Route(paths, output, content_type))
- def run(self):
- self.http.bind(self.host)
- self.http.listen(5)
- self.thread.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement