python – 强制使用IPv4 / IPv6的请求
发布时间:2020-12-16 23:01:00 所属栏目:Python 来源:网络整理
导读:如何强制请求库使用特定的Internet协议版本来获取get请求?或者这可以通过 Python中的另一种方法更好地实现?我可以,但我不想使用卷曲…… 澄清目的的例子: import requestsr = requests.get('https://my-dyn-dns-service.domain/?hostname=my.domain',auth
如何强制请求库使用特定的Internet协议版本来获取get请求?或者这可以通过
Python中的另一种方法更好地实现?我可以,但我不想使用卷曲……
澄清目的的例子: import requests r = requests.get('https://my-dyn-dns-service.domain/?hostname=my.domain',auth = ('myUserName','my-password')) 解决方法
我找到了一个简化的解决方案来强制urrlib3使用ipv4或ipv6. urrlib3使用此方法为Http和Https创建新连接.您可以在其中指定要使用的任何AF_FAMILY.
import socket import requests.packages.urllib3.util.connection as urllib3_cn def allowed_gai_family(): """ https://github.com/shazow/urllib3/blob/master/urllib3/util/connection.py """ family = socket.AF_INET if urllib3_cn.HAS_IPV6: family = socket.AF_INET6 # force ipv6 only if it is available return family urllib3_cn.allowed_gai_family = allowed_gai_family (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |