[Tutor] Talking to UDPServer
kent37 at tds.net
Tue Nov 1 19:55:51 CET 2005
Carroll, Barry wrote:
> Yes, that is exactly what I want. I need to write a program that
> communicates with an existing server, using the UDP protocol. This is
> my first time writing such a program, and I need help getting started.
Here is an example from Python Network Programming, by John Goerzen. It opens a UDP port, sends a message, then echoes any received text to the console. There is no higher-level support built in to Python, you just open a datagram socket and push data out. Google for "Python udp" for more examples.
# UDP Example - Chapter 2
import socket, sys, time
host = sys.argv
textport = sys.argv
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = int(textport)
# That didn't work. Look it up instread.
port = socket.getservbyname(textport, 'udp')
print "Enter data to transmit: "
data = sys.stdin.readline().strip()
print "Looking for replies; press Ctrl-C or Ctrl-Break to stop."
buf = s.recv(2048)
if not len(buf):
print "Received: %s" % buf
More information about the Tutor