Halaman

Senin, 09 Mei 2016

small router snippet

. Senin, 09 Mei 2016 .

  1. class Router(object):  
  2.   
  3.     def __init__(self, server):  
  4.         self.__routes = []  
  5.         self.__server = server  
  6.   
  7.     def addRoute(self, regexp, controller, action):  
  8.         self.__routes.append({'regexp': regexp, 'controller': controller, 'action': action})  
  9.           
  10.     def route(self, path):  
  11.         for route in self.__routes:  
  12.             if re.search(route['regexp'], path):  
  13.                 cls = globals()[route['controller']]  
  14.                 func = cls.__dict__[route['action']]  
  15.                 obj = cls(self.__server)  
  16.                 apply(func,(obj, ))  
  17.                 return  
  18.   
  19.         # Not found  
  20.         self.__server.send_response(404)  
  21.         self.__server.end_headers() 

0 komentar:

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Posting Komentar

 
{nama-blog-anda} is proudly powered by Blogger.com | Template by Agus Ramadhani | o-om.com