I was curious how hard it would be to implement a routing system like
flask uses. As it turn’s out it’s actually not that hard if you understand how decorator’s work.
The flask implementation has a lot of features like routes based on a regular expression. Mine just maps a path to a function.
#!/usr/bin/env python from http.server import BaseHTTPRequestHandler, HTTPServer import sys DEFAULT_PORT = 8080 LOCALHOST = '127.0.0.1' DEFAULT_ENCODING = 'utf-16' ROUTES = None class Routes(object): def __init__(self): self.routes = self.__dict__ global ROUTES ROUTES = self def add(self, route, cb): self.routes[route] = cb() def get(self, route): return self.routes.get(route) def exists(self, route): return route in self.routes def __call__(self, route): return self.get(route) class Route(object): def __init__(self, route): self.route = route def __call__(self, func): ROUTES.add(self.route, func) class RequestHandler(BaseHTTPRequestHandler): Routes() def send_msg(self, msg, encoding=DEFAULT_ENCODING): self.wfile.write(bytes(msg, encoding)) def do_GET(self): if not ROUTES.exists(self.path): self.send_header('content-type', 'text/html') self.send_response(404) self.end_headers() self.send_msg('<center><h1>404 😩</h1></center>') return self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.send_msg(ROUTES(self.path)) return def run(port=8080, server_addr=LOCALHOST): print('starting server...') server_address = (server_addr, port) httpd = HTTPServer(server_address, RequestHandler) print('running server...') httpd.serve_forever() if __name__ == '__main__': @Route('/') def root(): return '<h1><center><b>It Works 😁</b></center></h1>' run()
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!