detecting connect() failures on windows

Gregory P. Smith greg@e...
Fri, 26 Jan 2001 02:19:26 -0800


Has anybody had any success in getting asyncore under python on
windows to detect connect() failures?

On real OSes (linux and bsd) I get nice easy to understand behavior,
the attempt to connect to a host on a port its not listening on
produces this output: (code below)

......
creating foo to port 81
log: adding channel <foo at 81f89e0>
asyncore loop starting:
handle_connect
Exception during self.send("") in connect: (32, 'Broken pipe')
handle_read
Exception during self.recv in read: (111, 'Connection refused')
handle_write
handle_read
handle_close
log: closing channel 4:<foo connected at 81f89e0>
data read:

exited asyncore loop
......

On windows, it just hangs for a long time in select. (win2000 is my
main test case but it behaves just as badly on win98)


thanks,
-Greg


Here's my simple test program:




import asyncore
import socket

class foo(asyncore.dispatcher):
writ = 1
buf = 'GET /non.existant.file HTTP/1.0\r\n\r\n'

def handle_write(self):
print 'handle_write'
if not self.writ:
return
self.send(self.buf)
self.writ = 0
def writable(self):
return self.writ
def readable(self):
return 1
def handle_read(self):
print 'handle_read'
try:
data = self.recv(4096)
except socket.error, e:
print 'Exception during self.recv in read:', e
self.writ = 0 # read failed? we're closed now
return
print 'data read:'
print data
def handle_close(self):
print 'handle_close'
self.close()
#raise asyncore.ExitNow, 'handle_close called, exiting'
def handle_connect(self):
print 'handle_connect'
try:
data = self.send("")
except socket.error, e:
print 'Exception during self.send("") in connect:', e
return
def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, port))


print 'creating foo to port 80'
f1 = foo('www.mad-scientist.com', 80)
print 'asyncore loop starting:'
asyncore.loop()
print 'exited asyncore loop'


print 'creating foo to port 81'
f2 = foo('www.mad-scientist.com', 81)
print 'asyncore loop starting:'
asyncore.loop()
print 'exited asyncore loop'