python注册eureka服务

记录一下 前段时间 Python 做的一点东西  注册服务

 

# coding:utf-8
import tornado.httpserver
import tornado.ioloop
import tornado.options
import  requests
from tornado.web import RequestHandler
import py_eureka_client.eureka_client as eureka_client
from tornado.options import define, options
from lxml import  etree
from testpy.MysqlInsertDBS import MysqlInsertDBS


define("port", default=8002, help="run on the given port", type=int)

class IndexHandler(RequestHandler):
    def get(self):
        redu =MysqlInsertDBS.select()
        headers = {
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
        html = requests.get(redu[0], headers=headers).content.decode('utf-8')
        domtree = sprderUrl = etree.HTML(html)
        links = domtree.xpath(redu[1])
        k = links[0]
        i = k[0]
        for kk in i:
            text = kk.xpath(redu[2])
            href = kk.xpath(redu[3])
            img = kk.xpath(redu[4])
            multi_list = map(list, zip(text, href, img))
            for i in multi_list:
                MysqlInsertDBS.insert(i[0],i[1],i[2])


#restfulAPI功能实现在这里
class TestHandler(RequestHandler):
    def post(self):
        a = self.get_argument("siteList")
        b = self.get_argument("temp")
        c = self.get_argument("name")
        d = self.get_argument("url")
        print(a)
        print(b)
        print(c)
        print(d)
        self.write("hello world   welcome to you")

def eurekaclient():
    tornado.options.parse_command_line()
    # 注册eureka服务
    eureka_client.init_registry_client(eureka_server="http://localhost:7001/eureka/",
                                       app_name="python-spider",
                                       instance_port=8002)
    #提供外部调用的接口
    app = tornado.web.Application(handlers=[(r"/", IndexHandler),
                                            (r"/test", TestHandler)])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
    print("eureka exec")

if __name__ == "__main__":
    eurekaclient()

你可能感兴趣的:(Python)