This repository has been archived on 2021-09-15. You can view files and clone it, but cannot push or open issues or pull requests.
DUT2PTUT/superTornado.py

193 lines
6.7 KiB
Python
Raw Normal View History

2014-02-27 15:43:58 +01:00
import tornado.ioloop
import tornado.web
2014-02-27 16:55:33 +01:00
import tornado.httpserver
import tornado.websocket
2014-02-27 23:39:19 +01:00
import tornado.options
2014-03-02 23:07:43 +01:00
import sys
2014-02-28 06:55:36 +01:00
import time
2014-02-28 03:16:09 +01:00
import base64
import socket
2014-02-28 04:07:56 +01:00
from urllib import urlopen
from tornado.ioloop import PeriodicCallback
from m.loadConf import *
from m.login import *
from m.log import *
2014-02-28 11:20:21 +01:00
import os
2014-02-27 15:43:58 +01:00
config = LoadConf()
blind = False
camera = ""
port =""
ficLog = Log()
2014-02-27 22:45:07 +01:00
2014-03-02 21:53:08 +01:00
class BaseHandler(tornado.web.RequestHandler):
2014-02-27 23:12:23 +01:00
def get_current_user(self):
return self.get_secure_cookie("user")
2014-02-27 22:46:17 +01:00
2014-03-02 22:19:10 +01:00
def get_autorisation(self):
return self.get_secure_cookie("auth")
2014-02-27 23:12:23 +01:00
class MainHandler(BaseHandler):
2014-02-27 16:06:38 +01:00
def get(self):
self.render("v/index.html")
2014-03-02 21:10:30 +01:00
def post(self):
2014-02-27 18:43:55 +01:00
iden = self.get_argument("id","")
mdp = self.get_argument("mdp","")
2014-02-27 20:06:37 +01:00
login = Login()
autorise = login.connexion(iden, mdp)
print 'maison = httplib.HTTPConnection("192.168.16.150", 80)'
2014-03-02 22:19:10 +01:00
self.set_secure_cookie("user", iden)
2014-02-27 20:06:37 +01:00
if autorise == True:
2014-03-02 22:19:10 +01:00
self.set_secure_cookie("auth", "yes")
2014-02-27 23:12:23 +01:00
self.redirect("/video")
2014-02-27 20:10:46 +01:00
else:
print "->An unauthorized user try to access : " + self.request.remote_ip
self.redirect("/unauthorized")
2014-02-27 17:56:39 +01:00
class VideoHandler(BaseHandler):
2014-02-27 20:53:48 +01:00
def get(self):
2014-03-02 22:26:37 +01:00
if not self.get_autorisation and not self.get_current_user :
2014-02-27 23:12:23 +01:00
self.redirect("/")
return
2014-03-01 22:08:39 +01:00
self.render("v/video.html")
class UnauthorizedHandler(BaseHandler):
def get(self):
2014-03-02 22:26:37 +01:00
if not self.get_current_user :
2014-03-02 22:19:10 +01:00
self.redirect("/")
return
self.render("v/illegal.html")
def post(self):
force = self.get_argument("illegalAccess","")
if force == "1" :
2014-03-02 22:19:10 +01:00
self.set_secure_cookie("auth", "no")
self.redirect("/video")
else :
self.redirect("/")
class DisconnectionHandler(BaseHandler):
def post(self):
2014-03-02 22:19:10 +01:00
self.clear_cookie("auth")
self.clear_cookie("user")
self.redirect("/")
class WSocketHandler(BaseHandler,tornado.websocket.WebSocketHandler):
2014-02-28 06:01:10 +01:00
def open(self) :
2014-03-02 22:26:37 +01:00
if not self.get_autorisation and not self.get_current_user :
self.close()
2014-02-28 07:52:19 +01:00
return
2014-03-02 21:58:29 +01:00
print "->Websocket opened : " + self.request.remote_ip
2014-03-02 22:19:10 +01:00
iden = self.current_user
if self.get_autorisation == "yes":
2014-03-02 21:58:29 +01:00
ficLog.enregDansLog(iden,"Authorized user connection",self.request.remote_ip)
2014-03-01 17:41:24 +01:00
if confAveug == True:
print '->Send audio alarm authorized user'
print 'maison.request("GET", "micom/say.php?source=toto&text=Connection%20a%20la%20camera%20autorisee")'
else:
print '->Send visual alarm authorized user'
print 'maison.request("GET", "micom/lamp.php?room=salon1&order=1")'
2014-03-02 22:19:10 +01:00
print "->Authorized user access : " + self.request.remote_ip
else :
2014-03-02 22:26:37 +01:00
ficLog.enregDansLog(iden + " as IllegalUser","Unauthorized user connection",self.request.remote_ip)
2014-03-02 22:19:10 +01:00
if confAveug == True:
print '->Send audio alarm unauthorized user'
print 'maison.request("GET", "micom/say.php?source=toto&text=Connection%20a%20la%20camera%20non%20autorisee")'
else:
print '->Send visual alarm unauthorized user'
print 'maison.request("GET", "micom/lamp.php?room=salon1&order=1")'
print "->Unauthorized user access : " + self.request.remote_ip
2014-03-02 21:10:30 +01:00
self.send_image()
def on_message(self,mesg):
2014-03-02 22:19:10 +01:00
print "->Data receive : " + self.request.remote_ip
2014-03-02 21:10:30 +01:00
self.send_image()
2014-02-28 06:02:35 +01:00
def on_close(self):
2014-03-02 21:58:29 +01:00
print "->Websocket closed : "+self.request.remote_ip
2014-03-02 22:26:37 +01:00
iden = self.current_user
2014-03-02 22:19:10 +01:00
if self.get_autorisation == "yes":
2014-03-02 21:58:29 +01:00
ficLog.enregDansLog(iden,"Authorized user deconnection",self.request.remote_ip)
2014-03-02 22:19:10 +01:00
else :
2014-03-02 22:26:37 +01:00
ficLog.enregDansLog(iden + " as IllegalUser","Unauthorized user deconnection",self.request.remote_ip)
2014-03-01 17:37:17 +01:00
if confAveug == True:
print '->Send audio alarm deconnection user'
print 'maison.request("GET", "micom/say.php?source=toto&text=Connection%20a%20la%20camera%20rompue")'
else:
print '->Send visual alarm deconnection user'
print 'maison.request("GET", "micom/lamp.php?room=salon1&order=0")'
print"->"+iden+" Deconnection : " + self.request.remote_ip
2014-02-28 06:01:10 +01:00
2014-03-02 21:10:30 +01:00
def send_image(self) :
try :
socket.setdefaulttimeout(5)
f = urlopen('http://test:a@192.168.1.13/image.jpg?cidx=791836195')
2014-03-02 21:10:30 +01:00
data = f.read()
encoded = base64.b64encode(data)
self.write_message(encoded)
2014-03-02 22:19:10 +01:00
print "->Data send : " + self.request.remote_ip
2014-03-02 21:10:30 +01:00
except Exception, e :
print e
self.write_message("error")
2014-02-27 16:06:38 +01:00
application = tornado.web.Application([
(r"/", MainHandler),
2014-02-27 20:54:51 +01:00
(r"/video", VideoHandler),
(r"/unauthorized", UnauthorizedHandler),
(r"/disconnection", DisconnectionHandler),
(r"/socket", WSocketHandler),
(r"/style/(.*)", tornado.web.StaticFileHandler,{"path":"./v/style"},),
(r"/images/(.*)", tornado.web.StaticFileHandler,{"path":"./v/images"},),
(r"/js/(.*)", tornado.web.StaticFileHandler,{"path":"./v/js"},)],
2014-02-28 11:20:21 +01:00
cookie_secret="1213215656")
2014-02-27 15:43:58 +01:00
2014-02-27 15:51:19 +01:00
if __name__ == "__main__":
print "->Loading configuration ... "
try :
2014-03-02 23:26:33 +01:00
blind = config.isBlind()
ipCamera = config.ipCamera()
2014-03-02 23:48:39 +01:00
portCamera = config.portCamera()
portServ = config.portServ()
if blind == "error" :
raise BlindConfigurationERROR(bcolors.FAIL +"Failed Load Blind Configuration")
if ipCamera == "error" :
2014-03-02 23:49:34 +01:00
raise IPCameraConfigurationERROR(bcolors.FAIL +"Failed Load IP Camera Configuration")
if portCamera == "error" :
2014-03-02 23:49:34 +01:00
raise PortCameraConfigurationERROR(bcolors.FAIL + "Failed Load IP Camera Configuration")
if portServ == "error" :
2014-03-02 23:49:34 +01:00
raise PortServ ConfigurationERROR(bcolors.FAIL + "Failed Load Port Server Configuration")
except Exception, e :
print "Configuration Loading Failed ! Check Configuration File !" + bcolors.ENDC
print bcolors.FAIL + e + bcolors.ENDC
2014-03-02 23:03:14 +01:00
sys.exit(1)
print "->Configuration Server Load Successfully :"
if blind == True:
print " ->Blind unhabitant"
2014-02-27 19:56:31 +01:00
else :
print " ->Not blind unhabitant"
print " ->Ip camera : " + ipCamera
print " ->Port Server : " + portServ
2014-02-27 23:39:19 +01:00
tornado.options.parse_command_line()
2014-03-02 23:31:30 +01:00
try :
print("->Server Start ...")
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(port)
print "->Server Start Successfully !"
2014-03-02 23:31:30 +01:00
tornado.ioloop.IOLoop.instance().start()
except Exception, e :
print "Server Start Failed !"
print e
sys.exit(1)