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

Add possibility to specify IPv4 or IPv6? #1691

Closed
Fludizz opened this issue Oct 19, 2013 · 12 comments
Closed

Add possibility to specify IPv4 or IPv6? #1691

Fludizz opened this issue Oct 19, 2013 · 12 comments

Comments

@Fludizz
Copy link

Fludizz commented Oct 19, 2013

I noticed that request by default uses IPv6 if a host is capable of it. However, requests seems to lack the ability to force it to use either IPv6 or IPv4. Would it be possible to add an option that allows one to manually select the protocol?

@Lukasa
Copy link
Member

Lukasa commented Oct 19, 2013

That feature is handled way way down in the standard library, not even exposed by httplib, so isn't something we can easily provide access to. It's also phenomenally special-case, which makes us reluctant to add it to the library. However, if you really need to you can handle it yourself:

import socket

addrs = socket.getaddrinfo('www.google.com')
ipv4_addrs = [addr[4][0] for addr in addrs if addr[0] == socket.AF_INET]
ipv6_addrs = [addr[4][0] for addr in addrs if addr[0] == socket.AF_INET6]

At this point you can build your URL using the literal IP address rather than the hostname, remembering to correctly set the Host header. If you want to be really clever you can actually implement all of this logic inside a Transport Adapter.

@Lukasa Lukasa closed this as completed Oct 19, 2013
@hynekcer
Copy link

hynekcer commented Mar 19, 2014

I switched a dependency to requests from other package and now I have random problems with connectivity, because our network has a partial IPv6 connectivity only to the nearest router. The application connects to a cloud, where a node can sometimes reject a connection, but with a normal Round Robin DNS between IPv4 it is completely reliable.

The previous package (restkit) use a solution similar to

socket.socket(family,..).connect((host, port))

I think that it can be solved by subclassing HTTPConnection and by overriding its short connect method. I understand your reasons of reluctance. Do you think that a solution with Transport Adapter would be easier than subclassing HTTPConnection?

@Lukasa
Copy link
Member

Lukasa commented Mar 19, 2014

So the way I consider the question is this: if we subclass HTTPConnection, what API do we provide for accessing this feature?

Firstly, note that the subclassing will happen in urllib3, not here. urllib3 will be happy to take this I'm sure. Once that subclassing is done, Requests would need a way to provide an API to it. Interestingly, that API would probably end up being a TransportAdapter anyway. So it might be best just to skip the middle-man and write a special-case TransportAdapter.

That will be easier if you get your patch into urllib3, so I recommend you start there first.

@sigmavirus24
Copy link
Contributor

@Lukasa do you think an IPv6 Transport Adapter would be a good addition to the toolbelt?

@Lukasa
Copy link
Member

Lukasa commented Mar 19, 2014

I think a 'specify IP type' TA would be a good addition. It would be nice not to have to monkeypatch urllib3 to do it though. =)

@sigmavirus24
Copy link
Contributor

It would be nice not to have to monkeypatch urllib3 to do it though. =)

Agreed.

@wojons
Copy link

wojons commented Apr 15, 2014

Ran into needing this. I enabled ipv6 on my service so people with out ipv4 could connect. But i use the ipv4 addresses for something some users who have both find them selves a little confussed. would be great to add pinning to ip type

@wojons
Copy link

wojons commented Apr 29, 2014

I am not sure if the work around works when doing https?

@Lukasa
Copy link
Member

Lukasa commented Apr 29, 2014

Yeah, HTTPS is a problem here. Having a toolbelt adaptor would help.

@d-Rickyy-b
Copy link

Hi there, it's nearly 7 years later. Is there any easy, user friendly way to specify the IP version that requests should use?

@sigmavirus24
Copy link
Contributor

No

@melissaboiko
Copy link

here's a convenient requests wrapper by Alexander Schlarb: https://gitlab.com/-/snippets/1900824

@psf psf locked as resolved and limited conversation to collaborators Aug 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants