Timeout on a UDP Socket

w webbsoft2 at alltel.net
Sat Mar 20 23:29:54 EST 2004


I'm having problems with this program hanging on the revc(buf).  I was
trying to figure out a way to allow the socket to timeout.  I'm have
tried making the socket non-blocking but had on luck.  Being very new
to Python,  could there be a better way to accomplish this task.  The
program works very well, but on occassion is will not receive the data
from the server and hang.

Thank,


# Client program

import os.path

from socket import *
from time import *

# Set the socket parameters
#host = "localhost"
host = "10.0.0.20"
port = 21567
buf = 1024
addr = (host,port)

# Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)

# Send messages
while(1):
     if not os.path.exists( "user1.inp" ) :
		sleep( 2 )
     else:
		print "opening....."
		print "reading....."
		in_file = open("user1.inp", "r")

		data = in_file.readline()
		#print in_line
		in_file.close()

		if(UDPSock.sendto(data,addr)):
			print "Sending message '",data,"'.....<done>"


		if os.path.exists( "user1.inp" ) :
		   os.remove( "user1.inp" )

		print "Waiting for Response..."

		# Receive messages
                data = UDPSock.recv(buf)
		print "\nReceived message '", data,"'"

		#output result message from server
		f=open( "user1.out", "w" )
		f.write( data )
		f.close()


# Close socket
UDPSock.close()



More information about the Python-list mailing list