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

188 lines
6.6 KiB
Python
Raw Normal View History

2014-02-27 14:43:58 +00:00
import tornado.ioloop
import tornado.web
2014-02-27 15:55:33 +00:00
import tornado.httpserver
import tornado.websocket
2014-02-27 22:39:19 +00:00
import tornado.options
2014-03-02 22:07:43 +00:00
import sys
2014-02-28 05:55:36 +00:00
import time
2014-02-28 02:16:09 +00:00
import base64
import socket
2014-02-28 03:07:56 +00: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 10:20:21 +00:00
import os
2014-02-27 14:43:58 +00:00
config = LoadConf()
blind = False
2014-03-02 23:13:47 +00:00
ipCamera = ""
portCamera = ""
portServ =""
2014-03-03 12:02:51 +00:00
log = Log()
2014-02-27 21:45:07 +00:00
2014-03-02 20:53:08 +00:00
class BaseHandler(tornado.web.RequestHandler):
2014-02-27 22:12:23 +00:00
def get_current_user(self):
return self.get_secure_cookie("user")
2014-02-27 21:46:17 +00:00
2014-03-02 21:19:10 +00:00
2014-02-27 22:12:23 +00:00
class MainHandler(BaseHandler):
2014-02-27 15:06:38 +00:00
def get(self):
self.render("v/index.html")
2014-03-02 20:10:30 +00:00
def post(self):
2014-02-27 17:43:55 +00:00
iden = self.get_argument("id","")
mdp = self.get_argument("mdp","")
2014-02-27 19:06:37 +00:00
login = Login()
autorise = login.connexion(iden, mdp)
log.printL('maison = httplib.HTTPConnection("192.168.16.150", 80)',10)
2014-03-02 21:19:10 +00:00
self.set_secure_cookie("user", iden)
2014-02-27 19:06:37 +00:00
if autorise == True:
2014-03-03 14:08:39 +00:00
self.set_secure_cookie("user", iden)
2014-02-27 22:12:23 +00:00
self.redirect("/video")
2014-02-27 19:10:46 +00:00
else:
log.printL("->An unauthorized user try to access : " + self.request.remote_ip,30)
self.redirect("/unauthorized")
2014-02-27 16:56:39 +00:00
class VideoHandler(BaseHandler):
2014-02-27 19:53:48 +00:00
def get(self):
2014-03-02 21:26:37 +00:00
if not self.get_autorisation and not self.get_current_user :
2014-02-27 22:12:23 +00:00
self.redirect("/")
return
2014-03-01 21:08:39 +00:00
self.render("v/video.html")
class UnauthorizedHandler(BaseHandler):
def get(self):
self.render("v/illegal.html")
def post(self):
force = self.get_argument("illegalAccess","")
if force == "1" :
2014-03-03 14:08:39 +00:00
self.set_secure_cookie("user", "IllegalUser")
self.redirect("/video")
else :
self.redirect("/")
class DisconnectionHandler(BaseHandler):
def post(self):
self.clear_cookie("user")
self.redirect("/")
class WSocketHandler(BaseHandler,tornado.websocket.WebSocketHandler):
2014-02-28 05:01:10 +00:00
def open(self) :
2014-03-03 14:08:39 +00:00
if not self.get_current_user :
self.close()
2014-02-28 06:52:19 +00:00
return
log.printL("->Websocket opened : " + self.request.remote_ip,25)
2014-03-02 21:19:10 +00:00
iden = self.current_user
2014-03-03 14:11:38 +00:00
if iden != "IllegalUser":
2014-03-03 14:03:25 +00:00
log.printL("->"+iden + " : Authorized user connection : "+self.request.remote_ip,20)
2014-03-03 13:40:03 +00:00
if blind == True:
log.printL('->Send audio alarm authorized user',20)
log.printL('maison.request("GET", "micom/say.php?source=toto&text=Connection%20a%20la%20camera%20autorisee")',10)
2014-03-01 16:41:24 +00:00
else:
log.printL('->Send visual alarm authorized user',20)
log.printL('maison.request("GET", "micom/lamp.php?room=salon1&order=1")',10)
2014-03-02 21:19:10 +00:00
else :
2014-03-03 14:08:39 +00:00
log.printL("->"+iden + ": Unauthorized user connection : " + self.request.remote_ip,30)
2014-03-03 13:40:03 +00:00
if blind == True:
log.printL('->Send audio alarm unauthorized user',30)
log.printL('maison.request("GET", "micom/say.php?source=toto&text=Connection%20a%20la%20camera%20non%20autorisee")',10)
2014-03-02 21:19:10 +00:00
else:
log.printL('->Send visual alarm unauthorized user',30)
log.printL('maison.request("GET", "micom/lamp.php?room=salon1&order=1")',10)
2014-03-02 20:10:30 +00:00
self.send_image()
def on_message(self,mesg):
log.printL("->Data receive : " + self.request.remote_ip,20)
2014-03-02 20:10:30 +00:00
self.send_image()
2014-02-28 05:02:35 +00:00
def on_close(self):
log.printL("->Websocket closed : "+self.request.remote_ip,25)
2014-03-02 21:26:37 +00:00
iden = self.current_user
2014-03-03 14:11:38 +00:00
if iden != "IllegalUser":
2014-03-03 14:03:25 +00:00
log.printL("->"+iden+",Authorized user deconnection,"+self.request.remote_ip,20)
2014-03-02 21:19:10 +00:00
else :
2014-03-03 14:03:25 +00:00
log.printL("->"+iden +" as IllegalUser,Unauthorized user deconnection,"+self.request.remote_ip,30)
2014-03-01 16:37:17 +00:00
if blind == True:
log.printL('->Send audio alarm deconnection user', 20)
log.printL('maison.request("GET", "micom/say.php?source=toto&text=Connection%20a%20la%20camera%20rompue")',10)
2014-03-01 16:37:17 +00:00
else:
log.printL('->Send visual alarm deconnection user',20)
log.printL('maison.request("GET", "micom/lamp.php?room=salon1&order=0")',10)
print"->"+iden+" Deconnection : " + self.request.remote_ip
2014-02-28 05:01:10 +00:00
2014-03-02 20:10:30 +00: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 20:10:30 +00:00
data = f.read()
encoded = base64.b64encode(data)
self.write_message(encoded)
log.printL( "->Data send : " + self.request.remote_ip, 20)
2014-03-02 20:10:30 +00:00
except Exception, e :
log.printL(e.value,40)
2014-03-02 20:10:30 +00:00
self.write_message("error")
2014-02-27 15:06:38 +00:00
application = tornado.web.Application([
(r"/", MainHandler),
2014-02-27 19:54:51 +00: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 10:20:21 +00:00
cookie_secret="1213215656")
2014-02-27 14:43:58 +00:00
2014-02-27 14:51:19 +00:00
if __name__ == "__main__":
log.printL("->Loading configuration ... ",20)
try :
2014-03-02 22:26:33 +00:00
blind = config.isBlind()
2014-03-02 23:13:47 +00:00
ipCamera = config.ipCamera()
portCamera = config.portCamera()
portServ = config.portServ()
if blind == "error" :
raise ConfigError("Failed Load Blind Configuration")
if ipCamera == "error" :
raise ConfigError("Failed Load IP Camera Configuration")
if portCamera == "error" :
raise ConfigError("Failed Load IP Camera Configuration")
2014-03-02 23:13:47 +00:00
if portServ == "error" :
raise ConfigError("Failed Load Port Server Configuration")
except ConfigError as e :
log.printL(e.value,40)
log.printL("Configuration Loading Failed ! Check Configuration File !",40)
2014-03-02 22:03:14 +00:00
sys.exit(1)
log.printL("->Configuration Server Load Successfully !",25)
if blind == True:
log.printL(" +Blind unhabitant",20)
2014-02-27 18:56:31 +00:00
else :
log.printL(" +Not blind unhabitant",20)
log.printL(" +Ip Camera : " + ipCamera,20)
log.printL(" +Port Camera : " + portCamera,20)
log.printL(" +Port Server : " + portServ,20)
print ""
2014-03-02 22:31:30 +00:00
try :
2014-03-03 12:02:51 +00:00
log.printL("->Server Start ...",20)
tornado.options.parse_command_line()
2014-03-02 22:31:30 +00:00
http_server = tornado.httpserver.HTTPServer(application)
2014-03-02 23:13:47 +00:00
http_server.listen(portServ)
log.printL("->Server Start Successfully !",25)
2014-03-02 22:31:30 +00:00
tornado.ioloop.IOLoop.instance().start()
except Exception, e :
2014-03-03 12:02:51 +00:00
log.printL("Server Start Failed !",40)
log.printL(e.value,40)
2014-03-02 22:31:30 +00:00
sys.exit(1)