66#
77# By Bill Janssen.
88
9+ import re
10+ import os
11+ import ssl
912import 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+
7074if __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