# -*- coding : utf-8 -*- import os import time import base64 import threading import pymysql import logging import tornado.web import tornado.ioloop import tornado.options import tornado.httpserver import requests from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex from tornado.web import RequestHandler from quanliang_callback_rule import create_dialogue class CallBackHandle(RequestHandler): def get_querys(self): data = eval( self.request.body ) ##调用企微服务接口 t = threading.Thread(target=self.callback_qiwei_demo, args=(self.request.body,)) t.start() t2 = threading.Thread(target=self.callback_qiwei_server, args=(self.request.body,)) t2.start() logging.info( 'data:{}'.format(data['token']) ) appkey = data.get('app_key', '') token = data.get("token", '') nonce = data.get("nonce", '') timestamp = data.get("timestamp", '') encoding_content = data.get("encoding_content", '') signature = data.get("signature", '') connection = pymysql.connect(host="47.92.193.147", port=3306, user="root", passwd="Moxi123#", db="task_dialogue_config", charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) update_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(round(time.time() * 1000)) / 1000)) sql = 'insert into quanliang_quality_test ' \ '(appkey, token, nonce, timestamp, encoding_content, signature, update_time)' \ 'values ("{}", "{}", "{}", "{}", "{}", "{}", "{}");' cursor = connection.cursor() cursor.execute(sql.format(appkey, token, nonce, timestamp, encoding_content, signature, update_time)) connection.commit() encodingAESKey = "f4809cd6510d492791bcf316686f272a" # data = self.decrypt_cbc(content=encoding_content, encodingAESKey=encodingAESKey) def decrypt_cbc(self, content, encodingAESKey): key = bytearray(encodingAESKey.encode('utf-8')) iv = key[:16] cipher = AES.new(key, AES.MODE_CBC, iv) tmp = base64.b64decode(content) data = cipher.decrypt(tmp).decode('utf-8') return data def post(self): self.get_querys() def callback_qiwei_demo(self, data): try: url = "http://172.26.14.65:8891/callback" headers = { 'Content-Type': 'application/json;charset=UTF-8', } requests.post(url, data=data, headers=headers, timeout=5) logging.info("callback qiwei demo finish") except Exception as e: logging.error("callback other server error") def callback_qiwei_server(self, data): try: url = "http://172.26.14.65:8003/call_back" headers = { 'Content-Type': 'application/json;charset=UTF-8', } requests.post(url, data=data, headers=headers, timeout=5) logging.info("callback qiwei server finish") except Exception as e: logging.error("callback other server error") class QuanLiangHandle(RequestHandler): def get(self): min_date = self.get_argument('minDate', '') max_date = self.get_argument('maxDate', '') create_dialogue(min_date, max_date) if __name__ == '__main__': tornado.options.parse_command_line() app = tornado.web.Application([ (r'/callback', CallBackHandle), (r'/quanliang', QuanLiangHandle), ], static_path=os.path.join(os.path.dirname(__file__), "static"), template_path=os.path.join(os.path.dirname(__file__), "template"), debug=False ) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(9501) http_server.start(1) print("starting") tornado.ioloop.IOLoop.current().start() print("starting ... ")