Python TLS 客户端代码给出 Wrap Socket 意外关键字错误
Python TLS client code gives Wrap Socket unexpected keyword error
我正在尝试使用 following 客户端代码连接到使用 TLS 的服务器。(AES 256)
from socket import create_connection
import ssl
from ssl import SSLContext, PROTOCOL_TLS_CLIENT
hostname='MyHost'
ip = '10.98.1.1'
port = 11900
context = SSLContext(PROTOCOL_TLS_CLIENT)
context.load_verify_locations('client.pem')
with create_connection((ip, port)) as client:
# with context.wrap_socket(client, server_hostname=hostname) as tls:
with context.wrap_socket(client, ca_certs="ca.key", cert_reqs=ssl.CERT_REQUIRED, certfile="client.pem", keyfile="client.key") as tls:
print(f'Using {tls.version()}\n')
tls.sendall(b'Hello, world')
data = tls.recv(1024)
print(f'Server says: {data}')
我在 运行 时收到以下错误。在 Python 3.6/3.7 和 3.9
Traceback (most recent call last):
File "main.py", line 14, in <module>
with context.wrap_socket(client, ca_certs="ca.key", cert_reqs=ssl.CERT_REQUIRED, certfile="client.pem", keyfile="client.key") as tls:
TypeError: wrap_socket() got an unexpected keyword argument 'ca_certs'
根据 Googling I did,Python 3.7 似乎有问题,但我不明白为什么代码在 Python 3.6 中甚至不起作用。是 Python 有问题还是我使用函数调用不正确?
以下是使用 +TomerPlds 解决方案更新后的工作代码
from socket import create_connection
import ssl
from ssl import SSLContext, PROTOCOL_TLS_CLIENT
hostname='MyHost'
ip = '10.98.1.1'
port = 11900
context = SSLContext(PROTOCOL_TLS_CLIENT)
context.load_verify_locations('ca.pem')
with create_connection((ip, port)) as client:
# with context.wrap_socket(client, server_hostname=hostname) as tls:
with context.wrap_socket(client, server_hostname=hostname) as tls:
print(f'Using {tls.version()}\n')
tls.sendall(b'Hello, world')
while(True):
data = tls.recv(1024000000)
print(f'Server says: {data}')
意外关键字错误的原因是因为 SSLContext.wrap_socket 没有 ca_cert 参数,正如您在 the documentation 中看到的那样。
相反,您可以使用已经用于加载 CA 证书和客户端证书的 SSLContext.load_verify_locations。
顺便说一句,您似乎混合了 ssl.wrap_socket 和 SSLContext.wrap_socket 的参数,这就是错误参数的来源。
我正在尝试使用 following 客户端代码连接到使用 TLS 的服务器。(AES 256)
from socket import create_connection
import ssl
from ssl import SSLContext, PROTOCOL_TLS_CLIENT
hostname='MyHost'
ip = '10.98.1.1'
port = 11900
context = SSLContext(PROTOCOL_TLS_CLIENT)
context.load_verify_locations('client.pem')
with create_connection((ip, port)) as client:
# with context.wrap_socket(client, server_hostname=hostname) as tls:
with context.wrap_socket(client, ca_certs="ca.key", cert_reqs=ssl.CERT_REQUIRED, certfile="client.pem", keyfile="client.key") as tls:
print(f'Using {tls.version()}\n')
tls.sendall(b'Hello, world')
data = tls.recv(1024)
print(f'Server says: {data}')
我在 运行 时收到以下错误。在 Python 3.6/3.7 和 3.9
Traceback (most recent call last):
File "main.py", line 14, in <module>
with context.wrap_socket(client, ca_certs="ca.key", cert_reqs=ssl.CERT_REQUIRED, certfile="client.pem", keyfile="client.key") as tls:
TypeError: wrap_socket() got an unexpected keyword argument 'ca_certs'
根据 Googling I did,Python 3.7 似乎有问题,但我不明白为什么代码在 Python 3.6 中甚至不起作用。是 Python 有问题还是我使用函数调用不正确?
以下是使用 +TomerPlds 解决方案更新后的工作代码
from socket import create_connection
import ssl
from ssl import SSLContext, PROTOCOL_TLS_CLIENT
hostname='MyHost'
ip = '10.98.1.1'
port = 11900
context = SSLContext(PROTOCOL_TLS_CLIENT)
context.load_verify_locations('ca.pem')
with create_connection((ip, port)) as client:
# with context.wrap_socket(client, server_hostname=hostname) as tls:
with context.wrap_socket(client, server_hostname=hostname) as tls:
print(f'Using {tls.version()}\n')
tls.sendall(b'Hello, world')
while(True):
data = tls.recv(1024000000)
print(f'Server says: {data}')
意外关键字错误的原因是因为 SSLContext.wrap_socket 没有 ca_cert 参数,正如您在 the documentation 中看到的那样。 相反,您可以使用已经用于加载 CA 证书和客户端证书的 SSLContext.load_verify_locations。
顺便说一句,您似乎混合了 ssl.wrap_socket 和 SSLContext.wrap_socket 的参数,这就是错误参数的来源。