在python中配置tornado服务
Posted wdan2016
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在python中配置tornado服务相关的知识,希望对你有一定的参考价值。
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)
启动的服务
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"] = data
except:
response["data"] = data
self.finish(json.dumps(response, ensure_ascii=False))
为了防止重复加载数据模型,需要初始化
class Application(tornado.web.Application):
def init(self):
self.UtilsLTPTranslate = UtilsLTPTranslate()
handlers = [(r"/LTPTranslate", HandlerLTPTranslate)]
tornado.web.Application.init(self,handlers,)
def forService():
print("启动.............................")
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() #启动服务
以上是关于在python中配置tornado服务的主要内容,如果未能解决你的问题,请参考以下文章
python3 tornado开发TCP服务程序,也是666
使用python中的Tornado模块在handsontable中渲染自定义数据