Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gateray committed Oct 3, 2017
1 parent dfb9a3a commit 6c62f63
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
5 changes: 4 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
def make_app(routers, **kwargs):
class ExtApplication(Application):
def __init__(self, routers, **kwargs):
super(ExtApplication, self).__init__(routers, **kwargs)
self.redisConn = None
for urlSpec in routers:
if urlSpec.name not in enableList:
routers.remove(urlSpec)
super(ExtApplication, self).__init__(routers, **kwargs)
def getRedisConn(self):
try:
import tornadoredis
Expand All @@ -22,6 +22,7 @@ def getRedisConn(self):
self.redisConn = redisConn
return self.redisConn
except Exception:
self.redisConn = None
raise
def getSMTPConn(self):
try:
Expand All @@ -32,6 +33,8 @@ def getSMTPConn(self):
smtp.login(mail["username"], mail["password"])
return smtp
except Exception:
if smtp:
smtp.close()
raise

return ExtApplication(routers, **kwargs)
Expand Down
1 change: 1 addition & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
url(r"^/qywx/?", WeiXinQYHandler, name="qywx"),
url(r"^/sms/?", SMSHandler, name="sms"),
url(r"^/mail/?", EmailHandler, name="mail"),
url(r"^/ws/?", WebSocketHandler, name="websocket"),
]

appSettings = {
Expand Down
35 changes: 30 additions & 5 deletions views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from tornado.web import RequestHandler
from settings import *
from models import WeiXinQYMessage, SMSMessage, EmailMessage
from tornado.websocket import WebSocketHandler
from datetime import datetime

class BaseHandler(RequestHandler):
def prepare(self):
Expand All @@ -23,6 +25,10 @@ def hashdigest(text):
if enableSignature:
#获取所有请求参数
argsDict = self.request.arguments #{'timestamp': [b'12341234182'], 'test': [b'abc']}
# 验证时间戳
import time
if (time.time() - signatureTimeOutSecs) > int(argsDict.get('timestamp', [0])[0]):
raise Exception("signature has expired.")
#获取签名
signature = argsDict.pop('signature', None)
assert signature, "missing signature."
Expand All @@ -31,10 +37,6 @@ def hashdigest(text):
combineStr = "".join([ "%s%s"%(k,argsDict[k][0].decode()) for k in sorted(argsDict)]) + apiKey
#验证签名
assert (hashdigest(combineStr) == signature), "signature not match."
#验证时间戳
import time
if (time.time() - signatureTimeOutSecs) > int(argsDict['timestamp'][0]):
raise Exception("signature has expired.")

def get(self, *args, **kwargs):
self.write("it works!")
Expand Down Expand Up @@ -77,4 +79,27 @@ def post(self, *args, **kwargs):
)
emailMessage.addMessage(content, type="html")
yield emailMessage.send()
self.write("ok")
self.write("ok")

class WebSocketHandler(WebSocketHandler):
users = set()

def open(self):
self.users.add(self)
for u in self.users:
u.write_message("[%s]-[%s]-login."%(self.request.remote_ip,
datetime.now().strftime('%Y-%m-%d %H:%M:%S')))

def on_message(self, message):
for u in self.users:
u.write_message("[%s]-[%s]-send: %s"%(self.request.remote_ip,
datetime.now().strftime('%Y-%m-%d %H:%M:%S'), message))

def on_close(self):
self.users.remove(self)
for u in self.users:
u.write_message("[%s]-[%s]-logout."%(self.request.remote_ip,
datetime.now().strftime('%Y-%m-%d %H:%M:%S')))

def check_origin(self, origin):
return True

0 comments on commit 6c62f63

Please sign in to comment.