Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional host resolving #422

Closed
RafPe opened this issue Dec 17, 2015 · 2 comments
Closed

Conditional host resolving #422

RafPe opened this issue Dec 17, 2015 · 2 comments
Labels
duplicate Issue already exists

Comments

@RafPe
Copy link

RafPe commented Dec 17, 2015

Hey ,

First of all really great tool. Cannot imagine work without now :) What I would be looking for is what we have in cURL which allows resolve host name on the fly when making request

curl --resolve example.com:443:1.2.3.4 https://example.com

Is it already available and I'm missing something or could that be a feature request ?

@jkbrzt jkbrzt added enhancement New feature or enhancement decide labels Feb 28, 2016
@kfix
Copy link

kfix commented Jul 31, 2016

I do this with requests using this evil adapter I made:

from requests.adapters import HTTPAdapter
from types import MethodType
# http://docs.python-requests.org/en/master/_modules/requests/adapters/
class TransparentProxyAdapter(HTTPAdapter):
    # opens sockets to an arbitrary fixhost instead of deriving it from the URL's Host
    def __init__(self, tproxy, **kwargs):
       self.tproxy = tproxy
       self.timeout = 5
       super(TransparentProxyAdapter,self).__init__(**kwargs)

    def get_connection(self, url, proxies=None):
       # monkeypatch the connection pool's _new_conn()
       conn = super(TransparentProxyAdapter,self).get_connection(url, proxies)
       conn.fixhost = self.tproxy
       conn._new_conn = MethodType(self.monkeypatched_new_conn.__func__, conn, conn.__class__)
       return conn

    # https://github.com/shazow/urllib3/blob/1.16/urllib3/connectionpool.py#L208
    def monkeypatched_new_conn(self):
       # https://hg.python.org/cpython/file/2.7/Lib/httplib.py
       def monkeypatched_connect(self):
           orighost = self.host
           self.host = self.fixhost #switcheroo
           self.orig_connect()
           self.host = orighost #put it back

       self.num_connections += 1
       # monkeypatch the connection's connect()
       if self.fixhost != None and self.proxy == None:
          conn = self.ConnectionCls(self.host, self.port, timeout=self.timeout)
          conn.fixhost = self.fixhost
          conn.orig_connect = conn.connect
          conn.connect = MethodType(monkeypatched_connect, conn, conn.__class__)
       return conn

unfortunately urllib3 has no hooking to perform resolution statically and ignoring socket.getaddrinfo, which would be more comparable to how --resolve works with libcurl.

@gabetax
Copy link

gabetax commented Feb 15, 2017

Duplicate of #99

@Almad Almad added duplicate Issue already exists and removed considering enhancement New feature or enhancement labels Feb 3, 2021
@Almad Almad closed this as completed Feb 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issue already exists
Projects
None yet
Development

No branches or pull requests

5 participants