tornado+elasticsearch web API

1、简介

 最近使用tornado实现了一个对接ES库的API接口,tornado的优势在此不做讲解

下面分享一下核心代码,方便后面使用

tornado讲解推荐博客

2、code

import time
import tornado
import tornado.ioloop
import tornado.web
from tornado.escape import json_encode
from elasticsearch import Elasticsearch, RequestsHttpConnection


class MyConnection(RequestsHttpConnection):
    def __init__(self, *args, **kwargs):
        proxies = kwargs.pop('proxies', {})
        super(MyConnection, self).__init__(*args, **kwargs)
        self.session.proxies = proxies


class JsonUtils(object):

    @staticmethod
    def success(response, cost_time):
        """
        正确返回

        :param response: 返回结果
        :param cost_time: 耗时
        :return: string, {"status_msg": "success", "status_code": 0, "cost_time":, "response": }
        """
        return json_encode({"message": "success", "code": 0, "cost_time": cost_time, "data": response})

    @staticmethod
    def error(message, cost_time):
        """
        错误返回

        :param message: 错误信息
        :param cost_time: 耗时
        :return: string,
        """
        return json_encode({"message": message, "code": -1, "cost_time": cost_time, "data": None})


class Utils(object):
    """
    时间处理方法
    """

    def __init__(self):
        self.time_format = "%Y-%m-%d %H:%M:%S"
        self.es_client = None

    def search_data_by_id(self, id, index, _type):
        """ 从ES库中获取数据

        :param id: ID
        :param index: 索引名
        :param _type: 类型名
        :return: None or dict
        """
        if self.es_client is None:
            self.create_es_obj()

        try:
            response = self.es_client.get(index=index, doc_type=_type, id=id, ignore=[404])
        except Exception as e:
            print(e)
            return None
        else:
            if not response["found"]:
                return None
            else:
                return response['_source']

    def create_es_obj(self):
        try:
            # 使用代理时,必须python3 -m pip install -U requests[socks],不然requests不支持socks5
            self.es_client = Elasticsearch(["ip:port"],
                                           connection_class=MyConnection,
                                           proxies={'http': 'socks5://ip:port'},
                                           timeout=300,
                                           max_retries=10,
                                           retry_on_timeout=True)
        except Exception as e:
            print(e)
        else:
            print('创建ES连接成功...')


class IcpHandler(tornado.web.RequestHandler):
    def data_received(self, chunk):
        pass

    def initialize(self, handler):
        # 此函数将会自动被调用,接收传递参数
        self.handler = handler

    def post(self):
        # post 接口实现
        pass

    def get(self):
        """
        get方法实现

        处理正常返回:转换之后的时间字符串
        处理异常返回:错误信息
        """

        start_time = time.time()
        # 可以同时获取POST和GET请求参数
        domain = self.get_argument("value", default="")
        if not domain:
            msg = "missing parameter 'value' "
            result = JsonUtils.error(msg, time.time() - start_time)
        else:
            flag, result = self._process(domain)
            if not flag:
                result = JsonUtils.error(result, time.time() - start_time)
            else:
                result = JsonUtils.success({"data": result}, time.time() - start_time)
        self.write(result)

    def _process(self, domain):
        """
        时间转换方法

        :param domain: 域名
        :return: True, dict
        """
        try:
            return True, self.handler.search_data_by_id(domain.upper(), 'index', 'type')
        except Exception as e:
            return False, str(e)


class WebServerApplication(object):

    def __init__(self, port, utils_handler):
        self.port = port
        self.utils_handler = utils_handler

    def make_app(self):
        """
        构建Handler,
        (): 一个括号内为一个Handler
        """
        # 增加配置文件的方式
        # settings = {
        #     'debug': True,
        #     'static_path': os.path.join(os.path.dirname(__file__), "static"),
        #     'template_path': os.path.join(os.path.dirname(__file__), "template"),
        # }
        # application = tornado.web.Application([
        #     (r"/", MainHandler),
        # ], **settings)

        return tornado.web.Application([
            (r"/icp?", IcpHandler, dict(handler=self.utils_handler)),
        ], )

    def process(self):
        """
        构建app, 监听post, 启动服务

        """
        app = self.make_app()
        app.listen(self.port)
        tornado.ioloop.IOLoop.current().start()


if __name__ == "__main__":
    # 定义服务端口
    server_port = "8888"
    server = WebServerApplication(server_port, Utils())
    print("Begin server")
    server.process()

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值