On Feb 17, 12:15 pm, [EMAIL PROTECTED] (Douglas Wells) wrote: > > For example: > > > import socket, sys > > > host = 'localhost' #sys.argv[1] > > port = 3300 > > s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) > > > s.settimeout(1.0) > > buf = '' > > > data = 'hello world' > > num_sent = 0 > > > while num_sent < len(data): > > num_sent += s.sendto(data, (host, port)) > > > print "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > > while True: > > > try: > > buf, addr = s.recvfrom(2048) > > except: > > pass > > > #Will the following if statement do anything? > > # In this case it will cause the script to jump out of the loop > > # if it receives no data for a second. > > if not len(buf): > > break > > > print "Received from server: %s" % buf > > The reason that this example *seems* to work is that you mask the > exception. What is actually happening is that the timeout of the > recvfrom call raises socket.timeout, which is ignored. Then because > buf has been explicitly set to zero length (near the beginning of > the program), and because it is *not* modified as a result of the > recvfrom call, the length is still zero, and the break is executed. > Try commenting out the try/except construct, or try actually > providing at least one non-zero length response (such that buf is > modified) and seeing if it ever terminates. >
Nice catch. > I would also like to point out that the original example (quoted > from the book) used "connect' and "recv" w/ UDP). One of the > purposes of using this construct (rather than using "recvfrom") > is to simplify identification of the remote system: When you > "connect" to a UDP socket, the OS will only send messages to that > system and will ignore messages that do not originate from that > IP address (ignoring the issue IP address spoofing). > I was hashing through that very issue last night. I was wondering how sendall() knew where to send the data. The author says this about the initial UPD example I posted(the one that calls connect()): >...there's no actual connection here. The call to connect() > did nothing but initialize some internal parameters. I deduced that one such initialization was automatically resolving the hostname into an ip address. Thanks for the info on another important one. -- http://mail.python.org/mailman/listinfo/python-list