Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import time
- class Client:
- def __init__(self, address, port, timeout=0):
- self.address = address
- self.port = port
- self.timeout = timeout
- def put(self, metric, value, timestamp=0):
- timestamp = timestamp or int(time.time())
- with socket.create_connection((self.address, self.port), timeout=self.timeout) as sock:
- #sock.settimeout(self.timeout)
- try:
- #send_inf =
- sock.sendall(b'metric, value, timestamp')
- except socket.timeout:
- print('timeout error')
- except socket.error as ex:
- print('send data error', ex)
- def get(self, key):
- with socket.create_connection(key) as connect_sock:
- connect_sock.settimeout(self.timeout)
- try:
- connect_sock.sendall(key)
- receive_data = connect_sock.recv(4096)
- dict_data = map(key , receive_data)
- return dict_data
- except socket.error as ex:
- ClientError
- class ClientError:
- print('Socket error')
- if __name__ == '__main__':
- client = Client('127.0.0.1', 57721, 1)
- client.put("test", 0.5, timestamp=1)
- client.put("test", 2.0, timestamp=2)
- client.put("test", 0.5, timestamp=3)
- client.put("load", 3, timestamp=4)
- client.put("load", 4, timestamp=5)
- print(client.get("*"))
- #client.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement