100% found this document useful (1 vote)
98 views2 pages

Metric Sending Client Implementation

The document describes a Python client class for sending metrics to a server using sockets. It includes methods for putting and getting metrics, handling responses, and raising errors. The client connects to a specified host and port, and can send formatted messages to retrieve or store data.

Uploaded by

max
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
98 views2 pages

Metric Sending Client Implementation

The document describes a Python client class for sending metrics to a server using sockets. It includes methods for putting and getting metrics, handling responses, and raising errors. The client connects to a specified host and port, and can send formatted messages to retrieve or store data.

Uploaded by

max
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# Клиент для отправки метрик

import socket

class Client:
def __init__(self, host, port, timeout=None):
[Link] = host
[Link] = port
[Link] = timeout
with socket.create_connection(
([Link], [Link]),
[Link]
) as sock:
[Link] = sock

def put(self, key, value, timestamp):


message = 'put {key} {value} {timestamp}\n'.format(
key=key, value=value, timestamp=timestamp
)
[Link](message)

def get(self, key):


message = 'get {key}\n'.format(
key=key
)
response = [Link](message)
return response

def recieve(self, message):


[Link]([Link]('utf8'))
data = [Link](1024).decode('utf8')
array_of_lines = [Link]()
result = dict()
statuses = ['ok', 'error']
for line in array_of_lines:
if line:
line_data = [Link]()
if line_data[0] in statuses:
if line_data[0] == 'error':
raise ClientError
else:
if line_data[0] not in result:
result[line_data[0]] = list()
result[line_data[0]].append(
(int(line_data[2]), float(line_data[1]))
)
return result

class ClientError(Exception):
pass

def _main():
pass
'''
client = Client("[Link]", 8888, timeout=15)

[Link]("[Link]", 0.5, timestamp=1150864247)


[Link]("[Link]", 2.0, timestamp=1150864248)
[Link]("[Link]", 0.5, timestamp=1150864248)

[Link]("[Link]", 3, timestamp=1150864250)
[Link]("[Link]", 4, timestamp=1150864251)
[Link]("[Link]", 4200000)

print([Link]("*"))
'''

if __name__ == '__main__':
_main()

You might also like