python-etcd

 Client 對象

['_MDELETE', '_MGET', '_MPOST', '_MPUT', '__class__', '__contains__', '__del__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_allow_reconnect', '_allow_redirect', '_base_uri', '_check_cluster_id', '_comparison_conditions', '_del_conditions', '_discover', '_get_headers', '_handle_server_response', '_machines_cache', '_next_server', '_protocol', '_read_options', '_read_timeout', '_result_from_response', '_sanitize_key', '_stats', '_use_proxies', '_wrap_request', 'allow_redirect', 'api_execute', 'api_execute_json', 'base_uri', 'delete', 'election', 'eternal_watch', 'expected_cluster_id', 'get', 'get_lock', 'host', 'http', 'key_endpoint', 'leader', 'leader_stats', 'machines', 'members', 'password', 'pop', 'port', 'protocol', 'read', 'read_timeout', 'set', 'stats', 'store_stats', 'test_and_set', 'update', 'username', 'version_prefix', 'watch', 'write']node

 

     類屬性json

    httpapi

    

  實例屬性網絡

    allow_redirectapp

    base_uridom

    election              # Election primitives were removed from etcd 2.0ssh

    expected_cluster_id函數

    hostui

    key_endpointthis

    leader

    leader_stats

    machines

    members

    port

    protocol

    read

    read_timeout

    stats

    store_stats        

    version_prefix

 

  instancemethod 實例方法

    api_execute

    api_execute_json

    delete

    eternal_watch

    get

    get_lock

    pop

    set    

    test_and_set

    update

    watch

    write

 實例化對象

   函數原型:

    def __init__(
            self,
            host='127.0.0.1',         # mixed:類型-字符串(IP地址),元組((host,port),(host.port),...)  支持多個etcd服務端的鏈接。
            port=4001,            # 
            srv_domain=None,
            version_prefix='/v2',
            read_timeout=60,
            allow_redirect=True,
            protocol='http',
            cert=None,
            ca_cert=None,
            username=None,
            password=None,
            allow_reconnect=False,
            use_proxies=False,
            expected_cluster_id=None,
            per_host_pool_size=10
    ):

 

 實例: 

import etcd

# client
= etcd.Client() # this will create a client against etcd server running on localhost on port 4001 client = etcd.Client(port=4002) client = etcd.Client(host='127.0.0.1', port=4003) client = etcd.Client(host='127.0.0.1', port=4003, allow_redirect=False) # wont let you run sensitive commands on non-leader machines, default is true client = etcd.Client( host='127.0.0.1', port=4003, allow_reconnect=True, protocol='https',)

 

 

全局網絡鎖

 1 client = etcd.Client()
 2 lock = client.get_lock('/customer1', ttl=60)
 3  
 4 # Use the lock object:
 5 lock.acquire()
 6 lock.is_locked()  # True
 7 lock.renew(60)
 8 lock.release()
 9 lock.is_locked()  # False
10  
11 # The lock object may also be used as a context manager:
12 client = etcd.Client()
13 lock = client.get_lock('/customer1', ttl=60)
14 with lock as my_lock:
15     do_stuff()
16     lock.is_locked()  # True
17     lock.renew(60)
18 lock.is_locked()  # False
 1 # 建立etcd客戶端對象,
 2 # allow_redirect=True  這個參數是當斷鏈的時候,etcd會再次複用connect建立可用的鏈接
 3 client = etcd.Client(
 4              host='127.0.0.1',
 5              port=4003,
 6              allow_reconnect=True,
 7              protocol='https',)
 8 
 9 client.write('/nodes/n1', 1)
10
10 # with ttl 設置幾秒,幾秒後這數據就沒了 11 client.write('/nodes/n2', 2, ttl=4)   # sets the ttl to 4 seconds 12
12 # create only 13 client.write('/nodes/n3', 'test', prevExist=False) prevExist 若是數據有的話,就再也不插入了。 14 15 client.write('/nodes/n3', 'test2', prevValue='test1') 保證value之前是test1 16 17 # mkdir 18 client.write('/nodes/queue', dir=True) 新版本不用這個也行,最少我不用指明dir 也是能夠建立mkdir的 19
19 # Append a value to a queue dir 20 client.write('/nodes/queue', 'test', append=True) #will write i.e. /nodes/queue/11 21 client.write('/nodes/queue', 'test2', append=True) #will write i.e. /nodes/queue/12

 對於數據的修改也能夠用 update

1 result = client.read('/foo')
2 print(result.value) # bar
3 result.value += u'bar'
4 updated = client.update(result)  
5 print(updated.value)

用read()方法 從etcd獲取節點數據

client.read('/nodes/n2').value
 
#recursive遞歸的目錄  
#sorted 排序
r = client.read('/nodes', recursive=True, sorted=True)
for child in r.children:
    print("%s: %s" % (child.key,child.value))
 
#至關於zookeeper的watch監聽
client.read('/nodes/n2', wait=True) #Waits for a change in value in the key before returning.
client.read('/nodes/n2', wait=True, waitIndex=10)
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息