-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnixServer.py
66 lines (50 loc) · 1.64 KB
/
UnixServer.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# coding=utf-8
import socket
import os
import struct
import select
from threading import Thread
import threading
class UnixServer(Thread):
def __init__(self, queue, socket_path):
super().__init__()
self.queue = queue
self.socket_path = socket_path
self.shutdown_flag = threading.Event()
if os.path.exists(self.socket_path):
os.remove(self.socket_path)
self.server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.server.bind(self.socket_path)
os.chmod(self.socket_path, 666)
self.client = None
def recv_exact(self, length):
data = b""
while len(data) < length:
ret = self.client.recv(length)
if ret == b"":
return None
data += ret
return data
def kill(self):
self.shutdown_flag.set()
def run(self):
self.server.listen(1)
while not self.shutdown_flag.is_set():
readable, writable, errored = select.select([self.server], [], [], 0)
if len(readable) > 0:
self.client, _ = self.server.accept()
break
while not self.shutdown_flag.is_set():
datagram = self.recv_exact(4)
if datagram is None:
break
else:
sender, length = struct.unpack("<HH", datagram)
data = self.recv_exact(length)
self.queue.put_nowait([sender, data])
self.cleanup()
def cleanup(self):
if self.client is not None:
self.client.close()
self.server.close()
os.remove(self.socket_path)