You can not select more than 25 topics 
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long. 
		
		
		
		
		
			
	
	
		
			
	
		
			This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
		 
	
		
		
			
				
				
					
						
						
						
							---
 
						
						
						
							layout: post
 
						
						
						
							title: 如何用requests库验证证书
 
						
						
						
							tags: [Python, requests, ssl]
 
						
						
						
							---
 
						
						
						
							
 
						
						
						
							  用Python制作的程序怎么样? <!--more-->     
 
						
						
						
							
 
						
						
						
							# 起因
  
						
						
						
							, , , , , , ,  
						
						
						
							
 
						
						
						
							# 实现方案
  
						
						
						
							, , , , , ,  
						
						
						
							  不过我搜了一下, , , , , , ? : , , , , , , , ,  
						
						
						
							```python
  
						
						
						
							import  requests 
 
						
						
						
							import  hashlib 
 
						
						
						
							
 
						
						
						
							HTTPSConnection  =  requests . packages . urllib3 . connection . HTTPSConnection 
 
						
						
						
							orig_HTTPSConnection_connect  =  HTTPSConnection . connect 
 
						
						
						
							def  new_HTTPSConnection_connect ( self ): 
 
						
						
						
							    orig_HTTPSConnection_connect ( self ) 
 
						
						
						
							    try : 
 
						
						
						
							        self . peer_certificate  =  self . sock . getpeercert ( binary_form = True ) 
 
						
						
						
							    except  AttributeError : 
 
						
						
						
							        pass 
 
						
						
						
							HTTPSConnection . connect  =  new_HTTPSConnection_connect 
 
						
						
						
							
 
						
						
						
							def  verify_cert_request ( url ): 
 
						
						
						
							    with  requests . get ( url ,  stream = True ,  verify = False )  as  r : 
 
						
						
						
							        result  =  [  hashlib . sha256 ( r . raw . connection . sock . getpeercert ( binary_form = True )) . hexdigest (),  r . text  ] 
 
						
						
						
							    return  result 
 
						
						
						
							
 
						
						
						
							result  =  verify_cert_request ( 'https://www.baidu.com' ) 
 
						
						
						
							print ( result [ 0 ]) 
 
						
						
						
							print ( result [ 1 ][: 10 ]) 
 
						
						
						
							``` 
 
						
						
						
							  用这个代码就能获取到请求的网站中证书的指纹了, , , , ?  
						
						
						
							
 
						
						
						
							# 后记
  
						
						
						
							, ? , ?