Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

데블스캠프2011/다섯째날/PythonNetwork

From ZeroWiki
Revision as of 19:10, 1 July 2011 by imported>linflus
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

데블스캠프2011