Use the Python Bottle framework to write a simple service interface Example, pythonbottle

Source: Internet
Author: User
Tags zookeeper client

Use the Python Bottle framework to write a simple service interface Example, pythonbottle

Is there a scenario where a bunch of data is provided externally or a result is returned to the user. But I don't want to expose some internal data and logic to the other party... To put it simply, we want to provide an external interface as a service. There are many processing methods for this. RPC, what about building a web service .... However, these are all too important and difficult to operate. Here is a very easy way to handle it. Use bottle to solve the problem.

Requirement: Check whether some nodes in a zookeeper service exist. If OK is returned and no node exists, the node information is not saved. The returned information must be consistent with the results of pyunit.

Implementation environment:

1. python 2.7 and its built-in pyunit
2. bottle is a simple python server.

pip  install    bottle

3. kazoo a python zookeeper Client

pip install kazoo

1. Create a python test class zk_check.py

 -*- coding: utf-8 -*-                                                                                                                        from kazoo.client import KazooClient                       import unittest                                 class zktest(unittest.TestCase):                           def runTest(self):                                  zknamespace = “/app/zktest_performance_1”                     zkhosts = “127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183”        ZKTEST_DRIVERS = [“ip1”, “ip2”]            ZKTEST_NODES = [“ip3”, “ip4”, “ip5”, “ip6”]                                               driverChildren = []                               nodeChildren = []                                badDrivers = []                                 badNodes = []                                  # checking                                    zk = KazooClient(hosts=zkhosts, read_only=True)                 zk.start()                                    driverFatherPath = zknamespace + “/status/drivers”                nodeFatherPath = zknamespace + “/status/nodes”                                                                      if zk.exists(driverFatherPath):                           driverChildren = zk.get_children(driverFatherPath)                if len(driverChildren) >                                                              for driver in zktest_DRIVERS:                             if driver not in driverChildren:                           badDrivers.append(driver)                                                                                  if zk.exists(nodeFatherPath):                            nodeChildren = zk.get_children(nodeFatherPath)                  if len(nodeChildren) >                                                              for node in zktest_NODES:                               if node not in nodeChildren:                             badNodes.append(node)                                                                              zk.stop()      if (len(badNodes)==0) and (len(badDrivers)==0):                   self.assertEquals(1,1,”pass”)                        else:                                        if len(badDrivers) > 0:                               self.assertEquals(1,2,'len : %d , error : %s' % (len(badDrivers),badDrivers))        if len(badNodes) > 0:                                self.assertEquals(1,2,'len : %d , error : %s' % (len(badNodes),badNodes))                                         if __name__ == ‘__main__':                              unittest.main()

2. Write a bottle service and output the result.

  import commands                                                                                    from bottle import route, run, template                      @route(‘/alisa')                                  def index():                                      command = “python /Users/metaboy/script/zk_check.py”                 #output = os.popen(command)                             return template(‘<b>{{text}}</b>', text=commands.getoutput(command))                                                                     run(host='localhost', port=8888) 

3. Start the bottle service in the background to provide external access ip addresses

Now you can directly access it through http: // localhost: 8888/alisa.

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.