-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathads-server.py
More file actions
51 lines (35 loc) · 1.22 KB
/
ads-server.py
File metadata and controls
51 lines (35 loc) · 1.22 KB
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
49
50
51
import socket
import json
from opensky_api import OpenSkyApi
import time
host = 'localhost'
port = 5555
api = OpenSkyApi()
class Server(object):
def __init__(self,host,port):
self._host = host
self._port = port
def __enter__(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
sock.bind((self._host,self._port))
sock.listen(10)
self._sock = sock
return self._sock
def __exit__(self,*exc_info):
if exc_info[0]:
import traceback
traceback.print_exception(*exc_info)
self._sock.close()
with Server(host,port) as s:
while True:
conn, addr = s.accept()
time.sleep(10)
states = api.get_states(bbox=(45.8389, 47.8229, 5.9962, 10.5226))
try:
data = json.dumps([s.__dict__ for s in states.states]).encode('utf-8')
#print("Sent :" + str(len(data)) + " bytes")
conn.send(data)
except AttributeError:
print("Sent :" + "0 bytes")
conn.close()