-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudprecv.py
More file actions
43 lines (32 loc) · 786 Bytes
/
udprecv.py
File metadata and controls
43 lines (32 loc) · 786 Bytes
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
print("udp host ready")
import time
import random
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(False)
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8',1))
return s.getsockname()[0]
hostip = get_ip()
print("hostip:", hostip)
sock.bind((hostip, 1234))
count=0
remote=None
while True:
time.sleep(0.5)
count+=1
if remote:
r=random.randrange(5)
print('random',r)
if r==1:
sock.sendto(("%dmsg ok"%count).encode(), remote)
try:
data, addr = sock.recvfrom(1024)
if data:
remote=addr
print("msg:",data)
else:
print('no data')
except:
pass