Only close sockets if they have been created. Reported by Blake Winton.

This commit is contained in:
Martin v. Löwis 2001-10-07 08:53:32 +00:00
parent fb163784ab
commit 322c0d187d
5 changed files with 15 additions and 6 deletions

View File

@ -122,6 +122,7 @@ class FTP:
self.sock = socket.socket(af, socktype, proto) self.sock = socket.socket(af, socktype, proto)
self.sock.connect(sa) self.sock.connect(sa)
except socket.error, msg: except socket.error, msg:
if self.sock:
self.sock.close() self.sock.close()
self.sock = None self.sock = None
continue continue
@ -272,12 +273,14 @@ class FTP:
def makeport(self): def makeport(self):
'''Create a new socket and send a PORT command for it.''' '''Create a new socket and send a PORT command for it.'''
msg = "getaddrinfo returns an empty list" msg = "getaddrinfo returns an empty list"
sock = None
for res in socket.getaddrinfo(None, 0, self.af, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): for res in socket.getaddrinfo(None, 0, self.af, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
af, socktype, proto, canonname, sa = res af, socktype, proto, canonname, sa = res
try: try:
sock = socket.socket(af, socktype, proto) sock = socket.socket(af, socktype, proto)
sock.bind(sa) sock.bind(sa)
except socket.error, msg: except socket.error, msg:
if sock:
sock.close() sock.close()
sock = None sock = None
continue continue

View File

@ -368,6 +368,7 @@ class HTTPConnection:
except socket.error, msg: except socket.error, msg:
if self.debuglevel > 0: if self.debuglevel > 0:
print 'connect fail:', (self.host, self.port) print 'connect fail:', (self.host, self.port)
if self.sock:
self.sock.close() self.sock.close()
self.sock = None self.sock = None
continue continue

View File

@ -76,12 +76,14 @@ class POP3:
self.host = host self.host = host
self.port = port self.port = port
msg = "getaddrinfo returns an empty list" msg = "getaddrinfo returns an empty list"
self.sock = None
for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res af, socktype, proto, canonname, sa = res
try: try:
self.sock = socket.socket(af, socktype, proto) self.sock = socket.socket(af, socktype, proto)
self.sock.connect(sa) self.sock.connect(sa)
except socket.error, msg: except socket.error, msg:
if self.sock:
self.sock.close() self.sock.close()
self.sock = None self.sock = None
continue continue

View File

@ -265,6 +265,7 @@ class SMTP:
if not port: port = SMTP_PORT if not port: port = SMTP_PORT
if self.debuglevel > 0: print 'connect:', (host, port) if self.debuglevel > 0: print 'connect:', (host, port)
msg = "getaddrinfo returns an empty list" msg = "getaddrinfo returns an empty list"
self.sock = None
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res af, socktype, proto, canonname, sa = res
try: try:
@ -273,6 +274,7 @@ class SMTP:
self.sock.connect(sa) self.sock.connect(sa)
except socket.error, msg: except socket.error, msg:
if self.debuglevel > 0: print 'connect fail:', (host, port) if self.debuglevel > 0: print 'connect fail:', (host, port)
if self.sock:
self.sock.close() self.sock.close()
self.sock = None self.sock = None
continue continue

View File

@ -210,6 +210,7 @@ class Telnet:
self.sock = socket.socket(af, socktype, proto) self.sock = socket.socket(af, socktype, proto)
self.sock.connect(sa) self.sock.connect(sa)
except socket.error, msg: except socket.error, msg:
if self.sock:
self.sock.close() self.sock.close()
self.sock = None self.sock = None
continue continue