/usr/share/doc/libkyototycoon2/example/ktrestex.py is in kyototycoon-doc 0.9.56-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import time
import urllib
import http.client
# RESTful interface of Kyoto Tycoon
class KyotoTycoon:
# connect to the server
def open(self, host = "127.0.0.1", port = 1978, timeout = 30):
self.ua = http.client.HTTPConnection(host, port, False, timeout)
# close the connection
def close(self):
self.ua.close()
# store a record
def set(self, key, value, xt = None):
if isinstance(key, str): key = key.encode("UTF-8")
if isinstance(value, str): value = value.encode("UTF-8")
key = "/" + urllib.parse.quote(key)
headers = {}
if xt != None:
xt = int(time.time()) + xt
headers["X-Kt-Xt"] = str(xt)
self.ua.request("PUT", key, value, headers)
res = self.ua.getresponse()
body = res.read()
return res.status == 201
# remove a record
def remove(self, key):
if isinstance(key, str): key = key.encode("UTF-8")
key = "/" + urllib.parse.quote(key)
self.ua.request("DELETE", key)
res = self.ua.getresponse()
body = res.read()
return res.status == 204
# retrieve the value of a record
def get(self, key):
if isinstance(key, str): key = key.encode("UTF-8")
key = "/" + urllib.parse.quote(key)
self.ua.request("GET", key)
res = self.ua.getresponse()
body = res.read()
if res.status != 200: return None
return body
# sample usage
kt = KyotoTycoon()
kt.open("localhost", 1978)
kt.set("japan", "tokyo", 60)
print(kt.get("japan"))
kt.remove("japan")
kt.close()
|