# -*- coding: utf-8 -*- import os import json import time import datetime import hashlib import urllib.request import tornado.ioloop import tornado.web def md5Sign(params, secret, timestamp, connstr = '&', secret_connstr = ':'): params_str = '' for key in sorted(params.keys()): params_str = params_str + key + '=' + str(params[key]) + connstr if params_str != '' and connstr != '': params_str = params_str[0:-1] str1 = params_str + secret_connstr + str(timestamp) + secret try: m5 = hashlib.md5() m5.update(str1.encode('utf-8')) return m5.hexdigest() except Exception as e: print('md5Sign error: ' + str(e), flush=True) def getDaySeconds(time_val, incdays): time_zone = 8 dayseconds = int((time_val + time_zone * 3600)/3600/24 + incdays) * 3600 * 24 - 3600 * time_zone; print(dayseconds, flush=True) return dayseconds def sendNotify(conf, sendtime): try: print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%s"), flush=True) print('sendNotify start', flush=True) params = {'key' : 'kingsome'} secret = 'fc38349c5d084e920925e614c420be9f' timestamp = time.time() md5signstr = md5Sign(params, secret, timestamp) url = conf['notify_url'] + '×tamp=' + str(timestamp) + '&sign=' + md5signstr req = urllib.request.Request(url) data = urllib.request.urlopen(req).read() print('sendNotify end', flush=True) except Exception as e: print('sendNotifu error: ' + str(e), flush=True) #进入下一次循环 tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + sendtime, lambda : sendNotify(conf, sendtime)) class SelfCheckingHandler(tornado.web.RequestHandler): def get(self): self.write(json.dumps({ 'errcode': 0, 'errmsg': '', 'healthy': 1, 'max_rundelay': 10 }, separators=(',', ':'))) def make_app(): return tornado.web.Application([ (r"/webapp/index[\.]php", SelfCheckingHandler), ]) if __name__ == "__main__": print('start!', flush=True) conf = json.loads(open('../config/pay_backend.json', 'r').read()) app = make_app() app.listen(conf['listen_port']) tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + conf['sendtime1'], lambda : sendNotify(conf, conf['sendtime1'])) tornado.ioloop.IOLoop.current().start()