Py学习  »  Python

是否可以将端口号添加到python请求主机名?

user9371654 • 5 年前 • 1793 次点击  

我看过一些文章,其中建议要使用python请求指定端口号,只需将其添加到主机之后:

但是,如果我使用 requests.Session() ?

this post )

myCiphers = "AES128-SHA" +":"+  "AES256-SHA"

class myAdapter(HTTPAdapter):

  def init_poolmanager(self, *args, **kwargs):
      context = create_urllib3_context(ciphers=myCiphers)
      kwargs['ssl_context'] = context
      return super(myAdapter, self).init_poolmanager(*args, **kwargs)

  def proxy_manager_for(self, *args, **kwargs):
      context = create_urllib3_context(ciphers=defaultCiphers)
      kwargs['ssl_context'] = context
      return super(myAdapter, self).proxy_manager_for(*args, **kwargs)

myhost = 'https://google.com:443'
s = requests.Session()
s.mount(myhost,myAdapter())
response = s.get(myhost,verify=False,timeout=1)

请有经验的人确认一下,上面用挂载到请求会话的HTTP适配器指定端口号的方法,以及发出get请求时的方法是否正确?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53722
 
1793 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Martijn Pieters
Reply   •   1 楼
Martijn Pieters    6 年前

requests.Session() 匹配,大小写无关,由 一个人。如果希望适配器应用于以字符串开头的URL https://google.com:443 那你就用对了。

语法 :port :端口 违约 为了这个计划 使用。对于 https scheme,默认为端口443,因此使用 :443

但是,在安装适配器时,不会进行规范化,因此前缀 'https://google.com:443' 不匹配 https://google.com/ 即使两个地址的主机名和端口相同。