ValueError: check_hostname requires server_hostname
时间: 2023-07-22 13:50:26 浏览: 50
这个错误通常是在使用 urllib 或 requests 库发送 HTTPS 请求时出现的。它表示在进行 SSL/TLS 握手时缺少了必要的参数 server_hostname。
为了解决这个问题,你可以在请求中指定 server_hostname 参数,例如:
```python
import requests
url = 'https://www.example.com'
response = requests.get(url, verify=True, timeout=5, headers={}, stream=True, cert=None, proxies={}, auth=None, cookies={}, json=None, params=None, data=None, files=None, hooks=None, server_hostname='www.example.com')
```
如果你使用的是 urllib 库,则可以在创建 HTTPS 连接时指定 server_hostname 参数,例如:
```python
import urllib.request
import ssl
url = 'https://www.example.com'
context = ssl.create_default_context()
context.check_hostname = True
context.verify_mode = ssl.CERT_REQUIRED
with urllib.request.urlopen(url, context=context, timeout=5, cafile=None, capath=None, cadefault=False, context=None) as response:
html = response.read().decode('utf-8')
```
在这个例子中,我们使用了 ssl.create_default_context() 创建了一个 SSL 上下文对象,并将其 check_hostname 参数设置为 True,以确保在进行 SSL/TLS 握手时会使用 server_hostname 参数。同时,我们还将 verify_mode 参数设置为 ssl.CERT_REQUIRED,以强制验证服务器的证书。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)