UDP Demo with select()
# Based on Demo code from http://www.evolt.org/node/60276
# by Premshree Pillai, Mumbai University, India.
import select
import sys
from socket import *
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)
DUMPERS = {}
def dump(data, addr):
print "GOT <%s> from %s" % (data, addr)
print type(data)
UDPSock = socket(AF_INET,SOCK_DGRAM)
if sys.argv[1:] and sys.argv[1] == 's':
UDPSock.bind(addr)
DUMPERS[UDPSock] = dump
rd, wr, er = select.select([UDPSock], [], [], 999)
print rd, wr, er
data,addr = rd[0].recvfrom(buf)
DUMPERS[rd[0]](data, addr)
else:
data = "I wish I were an Oscar Meyer Wiener!"
UDPSock.sendto(data,addr)
Server prints:
[<socket._socketobject object at 0xb7d371ec>] [] []
GOT <I wish I were an Oscar Meyer Wiener!> from ('127.0.0.1', 58621)
<type 'str'>
(unless otherwise marked) Copyright 2002-2014 YakPeople. All rights reserved.