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    

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM