make it work
This commit is contained in:
parent
ccd9753276
commit
24a4126a8d
2 changed files with 7 additions and 6 deletions
2
demo.py
2
demo.py
|
@ -7,7 +7,7 @@ port = int(sys.argv[2])
|
|||
if sys.argv[1] == "s":
|
||||
sock.listen(("0.0.0.0", port))
|
||||
while True:
|
||||
for (d, a) in sock.poll():
|
||||
for d, a in sock.poll():
|
||||
print(d, a)
|
||||
time.sleep(0.1)
|
||||
elif sys.argv[1] == "c":
|
||||
|
|
11
network.py
11
network.py
|
@ -9,6 +9,8 @@ class Sock:
|
|||
self.sock_thread = None
|
||||
|
||||
def send(self, message, address):
|
||||
if type(message) == str:
|
||||
message = message.encode()
|
||||
self.sock.sendto(message, address)
|
||||
|
||||
def listen(self, address, length=65535):
|
||||
|
@ -16,7 +18,7 @@ class Sock:
|
|||
self.sock_thread.start()
|
||||
|
||||
def poll(self):
|
||||
if len(self.queue_lock) == 0:
|
||||
if len(self.queue) == 0:
|
||||
return []
|
||||
with self.queue_lock:
|
||||
queue = self.queue
|
||||
|
@ -25,14 +27,13 @@ class Sock:
|
|||
|
||||
class SockThread(Thread):
|
||||
def __init__(self, sock, address, length):
|
||||
super.__init__(super)
|
||||
Thread.__init__(self)
|
||||
self.sock = sock
|
||||
self.sock.sock.bind(address)
|
||||
self.length = self.length
|
||||
self.length = length
|
||||
self.sock.sock.bind(address)
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
r = self.sock.sock.recv(self.length)
|
||||
r = self.sock.sock.recvfrom(self.length)
|
||||
with self.sock.queue_lock:
|
||||
self.sock.queue.append(r)
|
||||
|
|
Loading…
Reference in a new issue