summaryrefslogtreecommitdiffstats
path: root/Tools/scripts/get-remote-certificate.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-03-30 06:33:02 (GMT)
committerGitHub <noreply@github.com>2019-03-30 06:33:02 (GMT)
commit172bb39452ae8b3ccdf5d1f23ead46f44200cd49 (patch)
tree5e1effbca3664b839a81eb7a7d62fa4974cfbfb1 /Tools/scripts/get-remote-certificate.py
parentafbb7a371fb44edc731344eab5b474ad8f7b57d7 (diff)
downloadcpython-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.zip
cpython-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.tar.gz
cpython-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.tar.bz2
bpo-22831: Use "with" to avoid possible fd leaks in tools (part 2). (GH-10927)
Diffstat (limited to 'Tools/scripts/get-remote-certificate.py')
-rwxr-xr-xTools/scripts/get-remote-certificate.py15
1 files changed, 6 insertions, 9 deletions
diff --git a/Tools/scripts/get-remote-certificate.py b/Tools/scripts/get-remote-certificate.py
index 5811f20..3890128 100755
--- a/Tools/scripts/get-remote-certificate.py
+++ b/Tools/scripts/get-remote-certificate.py
@@ -29,9 +29,8 @@ def fetch_server_certificate (host, port):
return None
else:
tn = tempfile.mktemp()
- fp = open(tn, "wb")
- fp.write(m.group(1) + b"\n")
- fp.close()
+ with open(tn, "wb") as fp:
+ fp.write(m.group(1) + b"\n")
try:
tn2 = (outfile or tempfile.mktemp())
status, output = subproc(r'openssl x509 -in "%s" -out "%s"' %
@@ -39,9 +38,8 @@ def fetch_server_certificate (host, port):
if status != 0:
raise RuntimeError('OpenSSL x509 failed with status %s and '
'output: %r' % (status, output))
- fp = open(tn2, 'rb')
- data = fp.read()
- fp.close()
+ with open(tn2, 'rb') as fp:
+ data = fp.read()
os.unlink(tn2)
return data
finally:
@@ -49,9 +47,8 @@ def fetch_server_certificate (host, port):
if sys.platform.startswith("win"):
tfile = tempfile.mktemp()
- fp = open(tfile, "w")
- fp.write("quit\n")
- fp.close()
+ with open(tfile, "w") as fp:
+ fp.write("quit\n")
try:
status, output = subproc(
'openssl s_client -connect "%s:%s" -showcerts < "%s"' %