-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch_client.py
More file actions
78 lines (61 loc) · 2.23 KB
/
ch_client.py
File metadata and controls
78 lines (61 loc) · 2.23 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from flask import Flask,jsonify,request,Response,json,Request
import requests
import json, bisect
import md5
"""Implement a consistent hashing ring."""
class ConsistentHashRing(object):
def __init__(self, replicas=1):
self.replicas = replicas
self._keys = []
self._nodes = {}
def _hash(self, key):
return long(md5.md5(key).hexdigest(), 16)
def _repl_iterator(self, nodename):
return (self._hash("%s:%s" % (nodename, i)) for i in xrange(self.replicas))
def __setitem__(self, nodename, node):
for hash_ in self._repl_iterator(nodename):
if hash_ in self._nodes:
raise ValueError("Node name %r is already present" % nodename)
self._nodes[hash_] = node
bisect.insort(self._keys, hash_)
def __delitem__(self, nodename):
for hash_ in self._repl_iterator(nodename):
del self._nodes[hash_]
index = bisect.bisect_left(self._keys, hash_)
del self._keys[index]
def __getitem__(self, key):
hashed_key = self._hash(key)
start = bisect.bisect(self._keys, hashed_key)
if start == len(self._keys):
start = 0
return self._nodes[self._keys[start]]
#Initializing hashing ring Object
cr = ConsistentHashRing()
cr.__setitem__("appinstance1","5000")
cr.__setitem__("appinstance2","6000")
cr.__setitem__("appinstance3","7000")
urllist = {}
for id in range(1,11):
'''making the POST requests'''
port = cr.__getitem__(str(id))
url = "http://192.168.99.100:%s/v1/expenses" % (port)
urllist[str(id)] = url
payload = {
"id": id,
"name": "Foo %s"%(id),
"email": "foo%s@bar.com"%(id),
"category": "office supplies",
"description": "iPad for office use",
"link": "http://www.apple.com/shop/buy-ipad/ipad-pro",
"estimated_costs": "700",
"submit_date": "12-10-2016"
}
r = requests.post(url, json=payload)
for id in range (1,11):
'''making the GET requests'''
port = cr.__getitem__(str(id))
url = "http://192.168.99.100:%s/v1/expenses/%s" % (port,id)
response = requests.get(url)
#data = json.loads(response.content)
print "Request: %s"%(id)
print response.text