-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtornado_conn.py
More file actions
103 lines (80 loc) · 2.77 KB
/
tornado_conn.py
File metadata and controls
103 lines (80 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
# coding: utf-8
import json
import time
import pika
import tornado.httpserver
import tornado.ioloop
import tornado.web
from pika_client import PikaClient
PORT = 8888
class BaseHandler(tornado.web.RequestHandler):
'''
Identify Client/Consumer
'''
pass
class MainHandler(BaseHandler):
@tornado.web.asynchronous
def get(self):
# main document
self.render("index.html")
class AjaxHandler(BaseHandler):
'''
Handler that serve server test request from the user browser.
post() method add request to it's queue
get() serve the long polling message system
'''
@tornado.web.asynchronous
def post(self):
# Receive the server test form information and post it to
# Rabbitmq request queue
self.application.pika.server_test(self.request, 'request')
# Send success output
self.set_header("Content-type", "application/json")
self.write(json.dumps({'status': 'Success'}))
self.finish()
@tornado.web.asynchronous
def get(self):
self.application.pika.add_listener(self, 'response')
def on_connection_close(self):
self.application.pika.remove_listener(self, 'response')
class TesterHandler(BaseHandler):
'''
Handler to serve server test requests for consumers.
get() serve the device with test information
post() method add server test result to it's queue
'''
@tornado.web.asynchronous
def post(self):
# Receive the server test result and add it to its queue
self.application.pika.server_test(self.request, 'response')
self.set_header("Content-type", "application/json")
self.write(json.dumps({'status': 'Success'}))
self.finish()
@tornado.web.asynchronous
def get(self):
self.application.pika.add_listener(self, 'request')
def on_connection_close(self):
self.application.pika.remove_listener(self, 'request')
if __name__ == '__main__':
# Setup the Tornado Application
settings = {"debug": True}
application = tornado.web.Application([
(r"/", MainHandler),
(r"/webtest", AjaxHandler),
(r"/webtest/server", TesterHandler)
], **settings)
# Helper class PikaClient
application.pika = PikaClient()
# Set our pika.log options
pika.log.setup(color=True)
# Start the HTTP Server
pika.log.info("Starting Tornado HTTPServer on port %i" % PORT)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(PORT)
# Get a handle to the instance of IOLoop
ioloop = tornado.ioloop.IOLoop.instance()
# Add our Pika connect to the IOLoop with a deadline in 0.1 seconds
ioloop.add_timeout(time.time() + .1, application.pika.connect)
# Start the IOLoop
ioloop.start()