summaryrefslogtreecommitdiffstats
path: root/Lib/logging/handlers.py
diff options
context:
space:
mode:
authorXiang Zhang <angwerzx@126.com>2017-06-01 13:11:56 (GMT)
committerGitHub <noreply@github.com>2017-06-01 13:11:56 (GMT)
commit0b4b57df964f1ba427684556b8e5f05852454e0d (patch)
treeea6569f8f9250da3c3eba30f033418c7856c4c98 /Lib/logging/handlers.py
parent4e624ca50a665d7e4d527ab98932347ff43a19b0 (diff)
downloadcpython-0b4b57df964f1ba427684556b8e5f05852454e0d.zip
cpython-0b4b57df964f1ba427684556b8e5f05852454e0d.tar.gz
cpython-0b4b57df964f1ba427684556b8e5f05852454e0d.tar.bz2
bpo-30378: Fix the problem that SysLogHandler can't handle IPv6 addresses (#1676)
Diffstat (limited to 'Lib/logging/handlers.py')
-rw-r--r--Lib/logging/handlers.py25
1 files changed, 20 insertions, 5 deletions
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
index 2356f8d..2f934b3 100644
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -827,11 +827,26 @@ class SysLogHandler(logging.Handler):
self.unixsocket = False
if socktype is None:
socktype = socket.SOCK_DGRAM
- self.socket = socket.socket(socket.AF_INET, socktype)
- if socktype == socket.SOCK_STREAM:
- self.socket.connect(address)
+ host, port = address
+ ress = socket.getaddrinfo(host, port, 0, socktype)
+ if not ress:
+ raise OSError("getaddrinfo returns an empty list")
+ for res in ress:
+ af, socktype, proto, _, sa = res
+ err = sock = None
+ try:
+ sock = socket.socket(af, socktype, proto)
+ if socktype == socket.SOCK_STREAM:
+ sock.connect(sa)
+ break
+ except OSError as exc:
+ err = exc
+ if sock is not None:
+ sock.close()
+ if err is not None:
+ raise err
+ self.socket = sock
self.socktype = socktype
- self.formatter = None
def _connect_unixsocket(self, address):
use_socktype = self.socktype
@@ -870,7 +885,7 @@ class SysLogHandler(logging.Handler):
priority = self.priority_names[priority]
return (facility << 3) | priority
- def close (self):
+ def close(self):
"""
Closes the socket.
"""