Python UDP server and client tutorial

UDP server

HOST = '192.168.1.1'
PORT = 7777
us = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
us.bind((HOST, PORT))
print 'UDP waiting to receive data'
while True:
data, (addr, port) = us.recvfrom( 1024 )
print 'Data:', data, ',  Connected:', addr, ":", port
us.sendto(data.upper(), (addr,port))	
us.close()

UDP client
HOST = '192.168.1.1'
PORT = 7777
uc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
say = raw_input('say something\r\n')
uc.sendto(str(say), (HOST,PORT))
received = uc.recv(1024)
print "Sent:     {}".format(say)
print "Received: {}".format(received)

0 意見: