ValueError: check_hostname requires server_hostname
时间: 2023-10-23 20:17:23 浏览: 321
This error occurs when the `check_hostname` parameter is set to `True` in a function that requires a `server_hostname` argument, but the `server_hostname` argument is not provided.
Here is an example of code that can produce this error:
```python
import ssl
import socket
context = ssl.create_default_context()
context.check_hostname = True
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_socket = context.wrap_socket(s)
ssl_socket.connect(('www.google.com', 443))
```
In this example, the `check_hostname` parameter is set to `True` in the SSL context object, but the `server_hostname` argument is not provided in the `connect()` method. To fix this error, we need to provide the `server_hostname` argument:
```python
import ssl
import socket
context = ssl.create_default_context()
context.check_hostname = True
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_socket = context.wrap_socket(s, server_hostname='www.google.com')
ssl_socket.connect(('www.google.com', 443))
```
In this updated code, we provide the `server_hostname` argument in the `wrap_socket()` method. This will resolve the `ValueError` and allow the SSL connection to be established.
阅读全文