Advertisement
Guest User

Untitled

a guest
Dec 5th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. import socket
  2. import threading
  3.  
  4. import routing
  5.  
  6. class Parvus(object):
  7.     def __init__(self, hostname, port=80):
  8.         self.http = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  9.         self.host = (hostname, port)
  10.         self.routes = dict()
  11.         self.thread = threading.Thread(None, self)
  12.  
  13.     def __call__(self):
  14.         while True:
  15.             client,address = self.http.accept()
  16.             threading.Thread(None, routing.handle_request, None, (client,address,self.routes)).start()
  17.  
  18.     def add_route(self, paths, output, **kwargs):
  19.         content_type = kwargs.pop("content_type", "text/plain")
  20.         for method in kwargs.pop("methods", ("GET",)):
  21.             method = method.upper()
  22.             if method not in self.routes:
  23.                 self.routes[method] = list()
  24.             self.routes[method].append(routing.Route(paths, output, content_type))
  25.  
  26.     def run(self):
  27.         self.http.bind(self.host)
  28.         self.http.listen(5)
  29.         self.thread.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement