mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
Patch #401196: Use getaddrinfo and AF_INET6 in TCP servers and clients.
This commit is contained in:
parent
d61e3eab44
commit
a43c2f845e
7 changed files with 160 additions and 49 deletions
|
@ -208,24 +208,32 @@ class SMTP:
|
|||
specified during instantiation.
|
||||
|
||||
"""
|
||||
if not port:
|
||||
i = host.find(':')
|
||||
if not port and (host.find(':') == host.rfind(':')):
|
||||
i = host.rfind(':')
|
||||
if i >= 0:
|
||||
host, port = host[:i], host[i+1:]
|
||||
try: port = int(port)
|
||||
except ValueError:
|
||||
raise socket.error, "nonnumeric port"
|
||||
if not port: port = SMTP_PORT
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
if self.debuglevel > 0: print 'connect:', (host, port)
|
||||
try:
|
||||
self.sock.connect((host, port))
|
||||
except socket.error:
|
||||
self.close()
|
||||
raise
|
||||
(code,msg)=self.getreply()
|
||||
if self.debuglevel >0 : print "connect:", msg
|
||||
return (code,msg)
|
||||
if self.debuglevel > 0: print 'connect:', (host, port)
|
||||
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
|
||||
af, socktype, proto, canonname, sa = res
|
||||
try:
|
||||
self.sock = socket.socket(af, socktype, proto)
|
||||
if self.debuglevel > 0: print 'connect:', (host, port)
|
||||
self.sock.connect(sa)
|
||||
except socket.error, msg:
|
||||
if self.debuglevel > 0: print 'connect fail:', (host, port)
|
||||
self.sock.close()
|
||||
self.sock = None
|
||||
continue
|
||||
break
|
||||
if not self.sock:
|
||||
raise socket.error, msg
|
||||
(code, msg) = self.getreply()
|
||||
if self.debuglevel > 0: print "connect:", msg
|
||||
return (code, msg)
|
||||
|
||||
def send(self, str):
|
||||
"""Send `str' to the server."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue