데블스캠프2011/다섯째날/PythonNetwork (rev. 1.1)
Python Client ¶
import socket
addr = ('255.255.255.255', 3333)
UDPSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Create socket
UDPSock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
print ('Enter your message:')
print ('- Empty message to stop this client.')
while True:
data = raw_input('>> ')
if len(data) == 0:
break
else:
if UDPSock.sendto(data, addr):
print ("Sending message '%s'..." % data)
UDPSock.close()
print ('Client stopped.')
import socket
rcv_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
rcv_sock.bind(("", 3333))
except:
print "Error at Binding"
while True:
print "Listening..."
data, addr = rcv_sock.recvfrom(3333)
print "Got %s" % data