-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
36 lines (29 loc) · 1.17 KB
/
backend.py
File metadata and controls
36 lines (29 loc) · 1.17 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
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import socket
'''
This is a simple Websockets Echo server that uses the Tornado websockets handler.
Please run `pip install tornado` with python of version 2.7.9 or greater to install tornado.
This program will echo back the reverse of whatever it receives.
Headers in the message are output to the terminal for debugging purposes.
'''
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
# Access headers received in the WebSocket request
headers = self.request.headers
print("WebSocket headers received:", headers)
def on_message(self, message):
print ('message received: ',message)
#print ('sending back message: ' % message[::-1])
self.write_message(message)
application = tornado.web.Application([
(r'/ws', WSHandler),
])
if __name__ == '__main__':
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
myIP = socket.gethostbyname(socket.gethostname())
print ('** Websockets Server Started at **' ,myIP)
tornado.ioloop.IOLoop.instance().start()