Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cbb9421

Browse files
committed
Fix bytes/str issues in get-remote-certificate.py.
1 parent 4d54088 commit cbb9421

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

Tools/ssl/get-remote-certificate.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
#
77
# By Bill Janssen.
88

9+
import re
10+
import os
11+
import ssl
912
import sys
13+
import tempfile
1014

11-
def fetch_server_certificate (host, port):
1215

13-
import re, tempfile, os, ssl
16+
def fetch_server_certificate (host, port):
1417

1518
def subproc(cmd):
1619
from subprocess import Popen, PIPE, STDOUT
@@ -20,15 +23,15 @@ def subproc(cmd):
2023
return status, output
2124

2225
def strip_to_x509_cert(certfile_contents, outfile=None):
23-
m = re.search(r"^([-]+BEGIN CERTIFICATE[-]+[\r]*\n"
24-
r".*[\r]*^[-]+END CERTIFICATE[-]+)$",
26+
m = re.search(br"^([-]+BEGIN CERTIFICATE[-]+[\r]*\n"
27+
br".*[\r]*^[-]+END CERTIFICATE[-]+)$",
2528
certfile_contents, re.MULTILINE | re.DOTALL)
2629
if not m:
2730
return None
2831
else:
2932
tn = tempfile.mktemp()
30-
fp = open(tn, "w")
31-
fp.write(m.group(1) + "\n")
33+
fp = open(tn, "wb")
34+
fp.write(m.group(1) + b"\n")
3235
fp.close()
3336
try:
3437
tn2 = (outfile or tempfile.mktemp())
@@ -67,6 +70,7 @@ def strip_to_x509_cert(certfile_contents, outfile=None):
6770
(host, port))
6871
return certtext
6972

73+
7074
if __name__ == "__main__":
7175
if len(sys.argv) < 2:
7276
sys.stderr.write(
@@ -75,5 +79,5 @@ def strip_to_x509_cert(certfile_contents, outfile=None):
7579
sys.exit(1)
7680
for arg in sys.argv[1:]:
7781
host, port = arg.split(":")
78-
sys.stdout.write(fetch_server_certificate(host, int(port)))
82+
sys.stdout.buffer.write(fetch_server_certificate(host, int(port)))
7983
sys.exit(0)

0 commit comments

Comments
 (0)