mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	Fix bytes/str issues in get-remote-certificate.py.
This commit is contained in:
		
							parent
							
								
									4d54088599
								
							
						
					
					
						commit
						cbb9421347
					
				
					 1 changed files with 11 additions and 7 deletions
				
			
		| 
						 | 
					@ -6,12 +6,15 @@
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# By Bill Janssen.
 | 
					# By Bill Janssen.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import re
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
 | 
					import ssl
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
 | 
					import tempfile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def fetch_server_certificate (host, port):
 | 
					def fetch_server_certificate (host, port):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    import re, tempfile, os, ssl
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def subproc(cmd):
 | 
					    def subproc(cmd):
 | 
				
			||||||
        from subprocess import Popen, PIPE, STDOUT
 | 
					        from subprocess import Popen, PIPE, STDOUT
 | 
				
			||||||
        proc = Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True)
 | 
					        proc = Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True)
 | 
				
			||||||
| 
						 | 
					@ -20,15 +23,15 @@ def subproc(cmd):
 | 
				
			||||||
        return status, output
 | 
					        return status, output
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def strip_to_x509_cert(certfile_contents, outfile=None):
 | 
					    def strip_to_x509_cert(certfile_contents, outfile=None):
 | 
				
			||||||
        m = re.search(r"^([-]+BEGIN CERTIFICATE[-]+[\r]*\n"
 | 
					        m = re.search(br"^([-]+BEGIN CERTIFICATE[-]+[\r]*\n"
 | 
				
			||||||
                      r".*[\r]*^[-]+END CERTIFICATE[-]+)$",
 | 
					                      br".*[\r]*^[-]+END CERTIFICATE[-]+)$",
 | 
				
			||||||
                      certfile_contents, re.MULTILINE | re.DOTALL)
 | 
					                      certfile_contents, re.MULTILINE | re.DOTALL)
 | 
				
			||||||
        if not m:
 | 
					        if not m:
 | 
				
			||||||
            return None
 | 
					            return None
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            tn = tempfile.mktemp()
 | 
					            tn = tempfile.mktemp()
 | 
				
			||||||
            fp = open(tn, "w")
 | 
					            fp = open(tn, "wb")
 | 
				
			||||||
            fp.write(m.group(1) + "\n")
 | 
					            fp.write(m.group(1) + b"\n")
 | 
				
			||||||
            fp.close()
 | 
					            fp.close()
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                tn2 = (outfile or tempfile.mktemp())
 | 
					                tn2 = (outfile or tempfile.mktemp())
 | 
				
			||||||
| 
						 | 
					@ -67,6 +70,7 @@ def strip_to_x509_cert(certfile_contents, outfile=None):
 | 
				
			||||||
                         (host, port))
 | 
					                         (host, port))
 | 
				
			||||||
    return certtext
 | 
					    return certtext
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    if len(sys.argv) < 2:
 | 
					    if len(sys.argv) < 2:
 | 
				
			||||||
        sys.stderr.write(
 | 
					        sys.stderr.write(
 | 
				
			||||||
| 
						 | 
					@ -75,5 +79,5 @@ def strip_to_x509_cert(certfile_contents, outfile=None):
 | 
				
			||||||
        sys.exit(1)
 | 
					        sys.exit(1)
 | 
				
			||||||
    for arg in sys.argv[1:]:
 | 
					    for arg in sys.argv[1:]:
 | 
				
			||||||
        host, port = arg.split(":")
 | 
					        host, port = arg.split(":")
 | 
				
			||||||
        sys.stdout.write(fetch_server_certificate(host, int(port)))
 | 
					        sys.stdout.buffer.write(fetch_server_certificate(host, int(port)))
 | 
				
			||||||
    sys.exit(0)
 | 
					    sys.exit(0)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue