|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: UTF-8 -*- |
| 3 | +# ----------------------------------------------------------------------------- |
| 4 | +# |
| 5 | +# P A G E B O T |
| 6 | +# |
| 7 | +# Copyright (c) 2016+ Buro Petr van Blokland + Claudia Mens |
| 8 | +# www.pagebot.io |
| 9 | +# Licensed under MIT conditions |
| 10 | +# |
| 11 | +# Supporting DrawBot, www.drawbot.com |
| 12 | +# Supporting Flat, xxyxyz.org/flat |
| 13 | +# ----------------------------------------------------------------------------- |
| 14 | +# |
| 15 | +# DemoServer.py |
| 16 | +# |
| 17 | +# https://www.tornadoweb.org |
| 18 | +# |
| 19 | +# Testing available parameters on the url page call. |
| 20 | +# This example server shows how different paths (detected by regular expressions) |
| 21 | +# are connected to different handlers, ranging from a simple request, reading a static |
| 22 | +# html page file, getting specific URL arguments (http://localhost:8886/query/aaa?n=100) |
| 23 | +# or disassembling the URL path by regular expressions. |
| 24 | +# This means that "parts" of the site can use different handlers than other parts. |
| 25 | +# |
| 26 | +# http://localhost:8889 |
| 27 | +# http://localhost:8889/blog |
| 28 | +# http://localhost:8889/query?n=100 |
| 29 | +# http://localhost:8889/query/aaa?n=100 |
| 30 | +# http://localhost:8889/resource/1234 |
| 31 | +# http://localhost:8889/resource/abcd-200/xyz-100 |
| 32 | + |
| 33 | +from tornado.web import Application, RequestHandler |
| 34 | +from tornado.ioloop import IOLoop |
| 35 | + |
| 36 | +PORT = 8889 |
| 37 | + |
| 38 | +class BasicRequestHandler(RequestHandler): |
| 39 | + def get(self): |
| 40 | + self.write('Hello, world') |
| 41 | + |
| 42 | +class StaticRequestHandler(RequestHandler): |
| 43 | + def get(self): |
| 44 | + self.render('templates_html/index.html') |
| 45 | + |
| 46 | +class QueryRequestHandler(RequestHandler): |
| 47 | + def get(self): |
| 48 | + n = self.get_argument('n') |
| 49 | + self.write('Argument is "%s"' % n) |
| 50 | + |
| 51 | +class ResourceRequestHandler(RequestHandler): |
| 52 | + def get(self, id): |
| 53 | + self.write('<h2>Querying path with id "%s"</h2>' % id) |
| 54 | + |
| 55 | +class PathRequestHandler(RequestHandler): |
| 56 | + def get(self, *args): |
| 57 | + for arg in args: |
| 58 | + self.write('<h2>Querying path item "%s"</h2>' % arg) |
| 59 | + |
| 60 | +if __name__ == '__main__': |
| 61 | + requestHandler = [ |
| 62 | + ('/', BasicRequestHandler), # http://localhost:8889 |
| 63 | + ('/blog', StaticRequestHandler), # http://localhost:8889/blog |
| 64 | + ('/query', QueryRequestHandler), # http://localhost:8889/query?n=100 |
| 65 | + ('/query/aaa', QueryRequestHandler), # http://localhost:8889/query/aaa?n=100 |
| 66 | + ('/resource/([0-9]+)', ResourceRequestHandler), # http://localhost:8889/resource/1234 |
| 67 | + ('/path/([A-Za-z0-9-]+)/([A-Z,a-z,0-9-]+)', PathRequestHandler), # http://localhost:8889/path/aaa/bbb |
| 68 | + ] |
| 69 | + app = Application(requestHandler) |
| 70 | + app.listen(PORT) |
| 71 | + print('Server on port', PORT) |
| 72 | + IOLoop.current().start() |
0 commit comments