-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
122 lines (98 loc) · 3.51 KB
/
server.py
File metadata and controls
122 lines (98 loc) · 3.51 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from tornado.ioloop import IOLoop
from tornado import gen
import tornado.web
import tornado.httpclient
import random
import sys
import httplib2
import urllib
http = httplib2.Http()
from tornado.log import enable_pretty_logging
enable_pretty_logging()
import time
import datetime
class CoinHandler(tornado.web.RequestHandler):
def get(self):
value=random.randint(0,1)
if (value):
coin = "Heads"
else:
coin="Tails"
self.write(coin)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello")
def post(self):
http_client = tornado.httpclient.HTTPClient()
post_data = {"jee": self.request.body[5:9]}
body = urllib.urlencode(post_data)
resp=http_client.fetch("http://localhost:8889/form/", method='POST', headers=None, body=body) #Send it off!
self.write(resp.body)
print "Mainhandler POST:" + resp.body
class MyFormHandler(tornado.web.RequestHandler):
def get(self):
self.write('<html><body><form action="/form/" method="POST">'
'<input type="text" name="message">'
'<input type="submit" value="Submit">'
'</form></body></html>')
def post(self):
req=self.request.body
self.write(req)
<<<<<<< HEAD
#self.set_header("Content-Type", "text/plain")
#self.write("You wrote " + self.get_body_argument("message"))
=======
print "Form handler POST: " + req
#self.set_header("Content-Type", "text/plain")
#self.write("You wrote " + self.get_body_argument("message"))
#send shit
class CoordinatorHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
#@gen.engine
def get(self):
print "Coordinator GET: Send request to Node "
#body = urllib.urlencode(data)
self.response,self.content= http.request("http://localhost:8889/node/","GET") #Send it off!
print "Coordinator GET response: " + self.content
self._async_callback(self.content)
def _async_callback(self, response):
print "Coordinator returns: " +response
self.write(response)
self.finish()
#tornado.ioloop.IOLoop.instance().stop()
class NodeHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
#@gen.engine
def post(self):
print "NODE POST: "
data=self.request.body
def get(self):
print "Node GET: "
#data=self.request.body
value=random.randint(0,1)
if (value):
coin = "Heads"
else:
coin="Tails"
#tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=5), self.get)
print "Returning"
self._async_callback(coin)
def _async_callback(self, response):
print "Node returns: "+response
self.write(response)
self.finish()
#tornado.ioloop.IOLoop.instance().stop()
def test():
print "Waiting for 5 seconds"
>>>>>>> 98784144650b2ad4abc3d2987b015a560c520eb5
if __name__ == "__main__":
application = tornado.web.Application([
(r"/", MainHandler),
(r"/coin/", CoinHandler),
(r"/form/", MyFormHandler),
(r"/coordinator/",CoordinatorHandler),
(r"/node/",NodeHandler)
],debug=True)
application.listen(int(sys.argv[1]))
print "Server in port: " + sys.argv[1]
tornado.ioloop.IOLoop.current().start()