U E D R , A S I H C RSS

데블스캠프2011/다섯째날/Python Network


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


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:12
Processing time 0.0139 sec