-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclass.py
39 lines (36 loc) · 1.17 KB
/
class.py
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
import time
import requests
class Communication:
def __init__(self, protocol, message_format, timeout, retry_limit):
self.protocol = protocol
self.message_format = message_format
self.timeout = timeout
self.retry_limit = retry_limit
def send_message(self, message):
retries = 0
while retries < self.retry_limit:
try:
response = requests.post(self.protocol, data=message, timeout=self.timeout)
return response
except requests.exceptions.Timeout:
retries += 1
print("Timeout occurred. Retrying...")
time.sleep(1)
except requests.exceptions.RequestException as e:
print("Error occurred: ", e)
break
return None
def receive_message(self):
retries = 0
while retries < self.retry_limit:
try:
response = requests.get(self.protocol, timeout=self.timeout)
return response
except requests.exceptions.Timeout:
retries += 1
print("Timeout occurred. Retrying...")
time.sleep(1)
except requests.exceptions.RequestException as e:
print("Error occurred: ", e)
break
return None