zoukankan      html  css  js  c++  java
  • python使用requests访问etcd

    由于本人项目中etcd的版本略低,不适用于python-etcd,etcd3这类第三方扩展包,所以呢,自己写了用requests请求的方法,放在这里,给需要的人。

    首先,etcd的请求可以使用网址的方式

    get key

    http://localhost:4001/key

    set key

    http://localhost:4001/key?value=123

    废话不多说,上代码

    class EtcdOperate:
    
        def __init__(self):
            """
              初始化
            """
            #基础url
            self.basic_url = 'http://localhost:4001'
    
        def get_key(self, key):
            """
            获取key内容
            :param key:
            :return:
            """
            try:
                url = '{}{}'.format(self.basic_url, key)
                response = requests.get(url)
                return response.text
            except Exception as ex:
                print("获取key值报错" + str(ex))
                return None
    
        def set_key(self, key, content):
            """
            写入信息
            :param key:
            :param content:
            :return:
            """
            try:
                url = '{}{}'.format(self.basic_url, key)
                params = {'value':content}
                response = requests.put(url,params=params)
                return response.text
            except Exception as ex:
                print("写入etcd'报错" + str(ex))
                return None    
    

      

  • 相关阅读:
    Flutter
    Flutter
    项目冲刺——第二天
    练习三:用例图
    项目冲刺——第一天
    作业六:团队项目冲刺前期准备
    作业四:软件案例分析
    作业五:需求规格说明书
    【非作业部分】队内暂时安排
    第二次练习——团队展示
  • 原文地址:https://www.cnblogs.com/yiyi8/p/12575101.html
Copyright © 2011-2022 走看看