Add possibility to specify IPv4 or IPv6?

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?


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:

``` python
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](http://docs.python-requests.org/en/latest/user/advanced/#transport-adapters).


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?


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.


@Lukasa do you think an IPv6 Transport Adapter would be a good addition to the [toolbelt](/sigmavirus24/requests-toolbelt)?


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. =)


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

Agreed. 


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


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


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


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

No

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