Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import socket
  2. import time
  3.  
  4.  
  5. class Client:
  6. def __init__(self, address, port, timeout=0):
  7. self.address = address
  8. self.port = port
  9. self.timeout = timeout
  10.  
  11. def put(self, metric, value, timestamp=0):
  12. timestamp = timestamp or int(time.time())
  13. with socket.create_connection((self.address, self.port), timeout=self.timeout) as sock:
  14. #sock.settimeout(self.timeout)
  15. try:
  16. #send_inf =
  17. sock.sendall(b'metric, value, timestamp')
  18. except socket.timeout:
  19. print('timeout error')
  20. except socket.error as ex:
  21. print('send data error', ex)
  22.  
  23. def get(self, key):
  24. with socket.create_connection(key) as connect_sock:
  25. connect_sock.settimeout(self.timeout)
  26. try:
  27. connect_sock.sendall(key)
  28. receive_data = connect_sock.recv(4096)
  29. dict_data = map(key , receive_data)
  30. return dict_data
  31. except socket.error as ex:
  32. ClientError
  33.  
  34.  
  35. class ClientError:
  36. print('Socket error')
  37.  
  38.  
  39. if __name__ == '__main__':
  40. client = Client('127.0.0.1', 57721, 1)
  41. client.put("test", 0.5, timestamp=1)
  42. client.put("test", 2.0, timestamp=2)
  43. client.put("test", 0.5, timestamp=3)
  44. client.put("load", 3, timestamp=4)
  45. client.put("load", 4, timestamp=5)
  46. print(client.get("*"))
  47.  
  48. #client.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement