Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c14e94

Browse files
Working on tornado server examples
1 parent d144674 commit 7c14e94

19 files changed

Lines changed: 612 additions & 7 deletions

Basics/00_Installing/css/test.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body{background-color:#c00}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
body {
2+
background-color: #cc0000; }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$theme_color: #cc0000;
2+
body {
3+
background-color: $theme_color;
4+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
# AdServer.py
16+
#
17+
# Using https://www.tornadoweb.org
18+
#
19+
# Using the PageBot Server to host a advertizement (Publication) generator.
20+
# URL parameters direct the type and content of the ad.
21+
#
22+
# http://localhost:8889
23+
# http://localhost:8889/blog
24+
# http://localhost:8889/query?n=100
25+
# http://localhost:8889/query/aaa?n=100
26+
# http://localhost:8889/resource/1234
27+
# http://localhost:8889/resource/abcd-200/xyz-100
28+
#
29+
from pagebot.server.baseserver import
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Tornado/PageBot AdServers
2+
3+
## AdServer create online advertizement servers
1.12 KB
Binary file not shown.

Sites/tornadoserver/BaseServer.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
# BaseServer.py
16+
#
17+
# https://www.tornadoweb.org
18+
#
19+
# Running this example script, creates a local tornado web server.
20+
# This example "Hello world" is not using PageBot, for simplicity of the code.
21+
# Open with a browser on http://localhost:7777
22+
# Each request shows the "counter" variable, as the number of page hits.
23+
# Stop the server with cntr-C, before running the script again.
24+
#
25+
import tornado.ioloop
26+
import tornado.web
27+
28+
counter = 0 # Counting the number of pages hits.
29+
30+
PORT = 7777 # The port that the server is listening to.
31+
32+
class MainHandler(tornado.web.RequestHandler):
33+
34+
def get(self):
35+
global counter
36+
self.write("Hello, %d world" % counter) # Answering the string (not even HTML)
37+
counter += 1
38+
39+
def data_received(self):
40+
pass
41+
42+
def make_app():
43+
return tornado.web.Application([
44+
(r"/", MainHandler), # Just listing to the main single http://localhost:7777 page.
45+
])
46+
47+
if __name__ == "__main__":
48+
app = make_app()
49+
app.listen(PORT)
50+
print('Starting Tornado web application on port %s' % PORT)
51+
tornado.ioloop.IOLoop.current().start()

Sites/tornadoserver/DemoServer.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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()

Sites/tornadoserver/HttpsServer.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
# httpsserver.py
16+
#
17+
# Showing certified https:// request.
18+
#
19+
# http://localhost:8882/blog
20+
# https://localhost/blog
21+
22+
import sys
23+
import tornado.httpserver
24+
import tornado.ioloop
25+
import tornado.web
26+
from httpshandler import HttpsHandler
27+
28+
PORT = 8882
29+
SSLPORT = 443
30+
31+
regex = r"/([^/]+)"
32+
33+
class HttpsServer:
34+
35+
def __init__(self, args=None, cert=None, key=None):
36+
#self.handlers = [('/', HttpsHandler),]
37+
self.handlers = [(regex, HttpsHandler),]
38+
39+
if args and '--port' in args:
40+
#try:
41+
port = int(args[-1])
42+
self.port = port
43+
#except Exception as e:
44+
# self.port = PORT
45+
# print(e)
46+
47+
else:
48+
self.port = PORT
49+
50+
self.app = tornado.web.Application(self.handlers)
51+
52+
if cert and key:
53+
ssl_options = {
54+
"certfile": cert,
55+
"keyfile": key,
56+
}
57+
58+
http_server = tornado.httpserver.HTTPServer(self.app, ssl_options=ssl_options)
59+
http_server.listen(SSLPORT)
60+
else:
61+
http_server = tornado.httpserver.HTTPServer(self.app)
62+
http_server.listen(self.port)
63+
#self.app.listen(self.port)
64+
65+
def run(self):
66+
print('Starting PageBot/Tornado web application on port %s' % self.port)
67+
tornado.ioloop.IOLoop.current().start()
68+
69+
if __name__ == "__main__":
70+
args = sys.argv[1:]
71+
server = HttpsServer(args=args)
72+
server.run()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
# BaseServer.py
16+
#
17+
# https://www.tornadoweb.org
18+
#
19+
# Running this example script, creates a local tornado web server.
20+
# This example "Hello world" is not using PageBot, for simplicity of the code.
21+
# Open with a browser on http://localhost:7777
22+
# Each request shows the "counter" variable, as the number of page hits.
23+
# Stop the server with cntr-C, before running the script again.
24+
#
25+
import tornado.ioloop
26+
import tornado.web
27+
28+
counter = 0 # Counting the number of pages hits.
29+
30+
PORT = 7777 # The port that the server is listening to.
31+
32+
class MainHandler(tornado.web.RequestHandler):
33+
34+
def get(self):
35+
global counter
36+
self.write("Hello, %d world" % counter) # Answering the string (not even HTML)
37+
counter += 1
38+
39+
def data_received(self):
40+
pass
41+
42+
def make_app():
43+
return tornado.web.Application([
44+
(r"/", MainHandler), # Just listing to the main single http://localhost:7777 page.
45+
])
46+
47+
if __name__ == "__main__":
48+
app = make_app()
49+
app.listen(PORT)
50+
print('Starting Tornado web application on port %s' % PORT)
51+
tornado.ioloop.IOLoop.current().start()

0 commit comments

Comments
 (0)