Configuring the Tornado service in Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.