Import Tornado.httpserver
Import Tornado.options
Import Tornado.web
From tornado.options import define, Options
From Data_util import utilsltptranslate
Define ("Port", default=8005, help= "run on the given port", Type=int)
Services started
Class Handlerltptranslate (Tornado.web.RequestHandler):
Def post (self):
Self.set_header (' Access-control-allow-origin ', ' * ')
Self.set_header (' access-control-allow-methods ', ' POST, GET, OPTIONS ')
Self.api_response ()
def api_response(self): post_data = {} response = {} data=[] try: for key in self.request.arguments: post_data[key] = self.get_arguments(key)[0] text = post_data["text"] if post_data["flag"] == "companyName": data.append(self.application.UtilsLTPTranslate.getEnCompanyName(text)) elif post_data["flag"] == "address": data.append(self.application.UtilsLTPTranslate.getEnAddress_2(text)) elif post_data["flag"] != "common" : data.append(self.application.UtilsLTPTranslate.zh2en(text)) else: data.append(self.application.UtilsLTPTranslate.zh2en(text)) response["data"] = dataexcept: response["data"] = dataself.finish(json.dumps(response, ensure_ascii=False))
To prevent the data model from being repeatedly loaded, you need to initialize
Class Application (Tornado.web.Application):
def Init(self):
Self. Utilsltptranslate = Utilsltptranslate ()
Handlers = [(R "/ltptranslate", Handlerltptranslate)]
Tornado.web.Application. Init (Self,handlers,)
Def forservice ():
Print ("Start ... ...................)
Tornado.options.parse_command_line ()
Http_server = Tornado.httpserver.HTTPServer (Application ())
Http_server.bind (Options.port)
Http_server.start (0)
Tornado.ioloop.IOLoop.instance (). Start ()
If name = = 'main':
Forservice () #启动服务
Configuring the Tornado service in Python