2017-12-05 13 views
0

urllib3を使用して、ホスト証明書を渡している間に、宛先ホストのIPアドレスを即座に無効にしようとしています。私のコードは以下の通りです:urllib3がホストIPアドレスをオーバーライドする

私はトランスポートアダプターを使用することを考えていましたが、セッションを使用せずにそれを行う方法はよく分かりません。

どのような考えや助け?

答えて

0

私たちはここで紹介するソリューションをたどることができますね。そこでPython 'requests' library - define specific DNS?

これを行うには厄介な方法を追加することによって、私たちが望むIPアドレスにホスト名を上書きすることです。もう一度、

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) 

をしかし、ポストされたリンクに基づいて、それを実装するより良い方法は、IPアドレスを上書きするための適切なアダプタを持つことです。

関連する問題