-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq_processor.py
50 lines (40 loc) · 1.36 KB
/
seq_processor.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
40
41
42
43
44
45
46
47
48
import socket
import requests
import time
producer_ip = "" or socket.gethostname()
producer_port = 8000
consumer_ip = "" or socket.gethostname()
consumer_port = 8001
data_size = 32
def processor():
try:
producer = socket.socket()
producer.connect((producer_ip, producer_port))
consumer = socket.socket()
consumer.connect((consumer_ip, consumer_port))
while True:
data = producer.recv(data_size).decode()
if data:
data = data.strip().strip('\x00')
url = f"https://pokeapi.co/api/v2/pokemon/{data}"
print(f"Processing for {data}: {url}")
r = requests.get(url)
consumer_data = str(r.status_code)
if r.ok:
json_data = r.json()
consumer_data += f":{json_data['name']}"
consumer.send(f"{consumer_data}".encode())
else:
producer.close()
consumer.close()
break
except ConnectionRefusedError:
print("Server not running!")
except ConnectionResetError:
print("Connection got broken!")
except ConnectionError:
print("Unable to establish connection!")
if __name__ == "__main__":
start_time = time.time()
processor()
print(f"Total time taken: {time.time() - start_time}")