Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Route:
- def __init__(self, route, action):
- self.route = route
- self.action = action
- class Router:
- def __init__(self):
- self.routes = []
- self.otherwise
- def when(self, route, action):
- route_object = Route(route, action)
- self.routes.append(route_object)
- def otherwise(self, action):
- self.otherwise = action
- def findAndCall(self, route):
- for r in self.routes:
- if r.route == route:
- r.action(route)
- return
- if self.otherwise:
- self.otherwise(route)
- def home_page(request):
- print("Welcome Home!")
- def other_page(request):
- print("Other Page!")
- def no_page(request):
- print("Page " + request + " not found!")
- router = Router()
- router.when("/", home_page)
- router.when("/other", other_page)
- router.otherwise(no_page)
- while True:
- route = input("http://mysite.com")
- router.findAndCall(route)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement