| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | #!/usr/local/bin/python3# -*- coding:utf-8 -*-# @Time    : 2018/5/18 3:18 PM# @Author  : Swingfrom tornado.web import RequestHandlerimport abuyun_renewimport statisticsimport tornado.ioloopimport email_utilimport tracebackimport jsonimport loop_taskclass MainHandler(RequestHandler):    def get(self, *args, **kwargs):        self.write('Hello world')    def post(self):        self.write('Hello world')class OpenChrome(RequestHandler):    def get(self, *args, **kwargs):        abuyun_renew.open_chrome()        self.write('Hello world')    def post(self):        self.write('Hello world')class ReCharge(RequestHandler):    def get(self, *args, **kwargs):        try:            abuyun_renew.recharge(is_launch_spider=True)            loop_task.loop_start()            self.finish(Response().to_json())        except:            email_util.send_email('Abuyun renew error', traceback.format_exc())            print('Daily statistic err! reason: ' + traceback.format_exc())            self.finish(Response(False, traceback.format_exc()).to_json())class DailyStatistic(RequestHandler):    def get(self, *args, **kwargs):        try:            statistics.daily_statistic()            self.finish(Response().to_json())        except:            email_util.send_email('Daily statistic err', traceback.format_exc())            print('Daily statistic err! reason: ' + traceback.format_exc())            self.finish(Response(False, traceback.format_exc()).to_json())class LoopTaskStart(RequestHandler):    def get(self, *args, **kwargs):        try:            loop_task.loop_start()            self.finish(Response().to_json())        except:            print(traceback.format_exc())class LoopTaskStop(RequestHandler):    def get(self, *args, **kwargs):        try:            loop_task.loop_stop()            self.finish(Response().to_json())        except:            print(traceback.format_exc())class Response(object):    def __init__(self, success=True, message='ok'):        self.success = success        self.message = message    def to_json(self):        return json.dumps(self, default=lambda obj: obj.__dict__, sort_keys=True, indent=4)application = tornado.web.Application([    (r"/openChrome", OpenChrome),    (r'/reCharge', ReCharge),    (r'/statistic', DailyStatistic),    (r'/loopStart', LoopTaskStart),    (r'/loopStop', LoopTaskStop),])if __name__ == "__main__":    application.listen(8888)    # print('Tornado start at port: 8888')    tornado.ioloop.IOLoop.instance().start()
 |