-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
59 lines (53 loc) · 1.97 KB
/
client.py
File metadata and controls
59 lines (53 loc) · 1.97 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
import requests
import time
from threading import Thread
import argparse
import random
class PyClient:
def __init__(self, server_url='localhost', port=8000, verbose=False):
self.server_url = f'http://127.0.0.1' if server_url == 'localhost' else server_url
self.verbose = verbose
self.port = port
def get_url(self):
if (random.random() > 0.5):
return self.server_url + ':' + str(self.port)
else:
return self.server_url + ':' + str(self.port)
def push(self, key, value):
message = key+','+value
response = requests.post(self.get_url() + '/push', data=message)
if response.text == "The Server Overloaded":
print("The Server Overloaded")
return
if self.verbose:
print('Received from server: ' + response.text)
return response.text
def pull(self):
response = requests.get(self.get_url() + '/pull')
print(response.text)
if response.text == "The Server Overloaded":
print("The Server Overloaded")
return "The Server Overloaded", "The Server Overloaded"
if self.verbose:
print('Received from server: ' + response.text)
if response.text != "no message":
return response.text.split(',')
return response.text
def subscribe_runner(self, url, f):
while True:
response = requests.get(self.get_url() + '/pull')
data = response.text
if data == "The Server Overloaded":
print("The Server Overloaded")
#kill the thread
break
if data == "no message":
time.sleep(1)
else:
data = data.split(',')
#print(data)
f(data[0], data[1])
time.sleep(0.01)
def subscribe(self, f):
thread = Thread(target=self.subscribe_runner, args=(self.server_url, f))
thread.start()