-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudptest.py
36 lines (30 loc) · 827 Bytes
/
udptest.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
import socket
import helper as h
import sys
thp = (socket.gethostname(), 5555)
tbo = ("<broadcast>", 5555)
cid = h.randomString(10)
# create an INET, UDP socket
udpsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
udpsocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
udpsocket.settimeout(5)
# bind the socket to a public host Server
#udpsocket.bind(thp)
if len(sys.argv) == 2:
msg = sys.argv[1]
else:
msg = h.randomString(5)
try:
udpsocket.sendto(msg, tbo)
#(msg,ipp) = udpsocket.recvfrom(1024)
print "Response: %s" % udpsocket.recv(1024)
except socket.timeout:
print "No server found"
while msg != "stop":
try:
(msg,ipp) = udpsocket.recvfrom(1024)
print msg
except socket.timeout:
pass
udpsocket.sendto(h.randomString(3),tbo)
udpsocket.close()