-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommunication.py
More file actions
45 lines (34 loc) · 1.13 KB
/
communication.py
File metadata and controls
45 lines (34 loc) · 1.13 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
import socket
import threading
class Communication(object):
_buffer = []
HOST = "25.67.72.254"
PORT = 3000
@staticmethod
def start():
threading.Thread(target=Communication.listen).start()
@staticmethod
def listen():
while True:
svr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
svr.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
svr.bind((Communication.HOST, Communication.PORT))
svr.listen(5)
con, adr = svr.accept()
data = con.recv(10*1024*1024)
Communication._buffer.append(data)
print(data.decode('utf-8'))
con.close()
@staticmethod
def receive():
def getData():
return Communication._buffer.pop(0).decode("utf-8")
if Communication._buffer:
return ''.join([x for x in list(getData())])
return []
@staticmethod
def send(data):
skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
skt.connect((Communication.HOST, Communication.PORT))
skt.send(bytes(data.encode('utf-8')))
skt.close()