2017-12-05 3 views
0

클라이언트 인증서를 전달하는 동안 urllib3을 사용하여 대상 호스트의 IP 주소를 즉시 무시하려고합니다. 여기 내 코드는 다음과 같습니다.urllib3가 호스트 IP 주소를 대체합니다.

import urllib3 
conn = urllib3.connection_from_url('https://MYHOST', ca_certs='ca_crt.pem', key_file='pr.pem', cert_file='crt.pem', cert_reqs='REQUIRED') 
response = conn.request('GET', 'https://MYHOST/OBJ', headers={"HOST": "MYHOST"}) 
print(response.data) 

전송 어댑터를 사용하려고 생각했지만 세션을 사용하지 않고 어떻게해야하는지 잘 모르겠습니다.

어떤 생각이나 도움이 필요하십니까?

답변

0

나는 우리가 여기에 제시된 해결책을 따를 수 추측 : 다시,

from urllib3.util import connection 
import urllib3 

hostname = "MYHOST" 
host_ip = "10.10.10.10" 
_orig_create_connection = connection.create_connection 

def patched_create_connection(address, *args, **kwargs): 
    overrides = { 
     hostname: host_ip 
    } 
    host, port = address 
    if host in overrides: 
     return _orig_create_connection((overrides[host], port), *args, **kwargs) 
    else: 
     return _orig_create_connection((host, port), *args, **kwargs) 

connection.create_connection = patched_create_connection 

conn = urllib3.connection_from_url('https://MYHOST', ca_certs='ca_crt.pem', key_file='pr.pem', cert_file='crt.pem', cert_reqs='REQUIRED') 
response = conn.request('GET', 'https://MYHOST/OBJ', headers={"HOST": "MYHOST"}) 
print(response.data) 

을하지만 이렇게하는 불쾌한 방법 따라서

Python 'requests' library - define specific DNS?을 추가하여 우리가 원하는 IP 주소로 호스트 이름을 무시하는 것입니다 게시 된 링크를 기반으로 구현하는 더 좋은 방법은 적절한 어댑터를 사용하여 IP 주소를 무시하는 것입니다.

관련 문제