Python
curl
HTTPS

HTTPS 通信したい。ただし IP を狙いうちたい

curl の場合

curl -H 'Host: www.hoge.com' --resolve 'www.hoge.com:443:1.1.1.1' https://www.hoge.com/

元ネタ

Python

forcediphttpsadapterを使うとできる
https://www.tomechangosubanana.com/2017/forcing-python-requests-to-connect-to-a-specific-ip-address/

import requests
from adapters import ForcedIPHTTPSAdapter

url = 'https://hoge.com'
s = requests.Session()
s.mount(url, ForcedIPHTTPSAdapter(dest_ip='1.1.1.1'))

s.get(url)

できたと思ったら、以下のようなエラーが

__new__() got an unexpected keyword argument 'key_dest_ip'
__new__() got an unexpected keyword argument 'key_dest_ip'
Traceback (most recent call last):
  File "/python/lib/python2.7/site-packages/requests/sessions.py", line 555, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/python/lib/python2.7/site-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/python/lib/python2.7/site-packages/requests/sessions.py", line 625, in send
    r = dispatch_hook('response', hooks, r, **kwargs)
  File "/python/lib/python2.7/site-packages/requests/hooks.py", line 31, in dispatch_hook
    _hook_data = hook(hook_data, **kwargs)
  File "/python/lib/python2.7/site-packages/requests/auth.py", line 257, in handle_401
    _r = r.connection.send(prep, **kwargs)
  File "/python/lib/python2.7/site-packages/requests/adapters.py", line 405, in send
    conn = self.get_connection(request.url, proxies)
  File "/python/lib/python2.7/site-packages/requests/adapters.py", line 309, in get_connection
    conn = self.poolmanager.connection_from_url(url)
  File "/python/lib/python2.7/site-packages/urllib3/poolmanager.py", line 279, in connection_from_url
    pool_kwargs=pool_kwargs)
  File "/python/lib/python2.7/site-packages/urllib3/poolmanager.py", line 227, in connection_from_host
    return self.connection_from_context(request_context)
  File "/python/lib/python2.7/site-packages/urllib3/poolmanager.py", line 238, in connection_from_context
    pool_key = pool_key_constructor(request_context)
  File "/python/lib/python2.7/site-packages/urllib3/poolmanager.py", line 103, in _default_key_normalizer
    return key_class(**context)
TypeError: __new__() got an unexpected keyword argument 'key_dest_ip'

Digest 認証するリクエストの場合でるのはわかったけど、、、
とりあえず、dest_ip が余計なのはわかったので、以下のように修正。

100 行目
kwargs['dest_ip'] = self.dest_ip ⇒ コメントアウト

101 行目
return ForcedIPHTTPSConnectionPool(host, port, **kwargs)
↓
return ForcedIPHTTPSConnectionPool(host, port, destip=self.dest_ip, **kwargs)
⇒ destip を引数に追加

107 行目
self.dest_ip = kwargs.pop('dest_ip', None)
↓
self.dest_ip = kwargs.pop('destip', None)

さらに Timeout の validate でもエラーが出たので urllib3 の import 分にくっついてる requests.packages をことごとく削除
(多分最初 rpm でインストールして、そのあと pip でアップデートとかだとでるっぽい)

その他

/etc/hosts に書く