Fabric中Connection.open()传入connect_kwargs参数报错求助
问题
尝试连接服务器时遇到SSH认证错误,于是在Fabric的Connection对象的open()方法中传入connect_kwargs={'password': 'LOSER'}参数,代码如下:
from fabric import Connection import socket uinaaaa = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) uinaaaa.connect(("8.8.8.8", 80)) xin = uinaaaa.getsockname()[0] PERSONA = Connection(f"user@{xin}") PERSONA.open(connect_kwargs={'password': "LOSER"}) print("Ok, now you will learn all the server's commands!") YUIA = PERSONA.run("help") print(YUIA.standout) print("If you want to learn more, type man, after that, a space, and then the command to learn more!")
运行后出现报错:
Traceback (most recent call last): File "main.py", line 7, in <module> PERSONA.open(connect_kwargs={'password': "LOSER"}) TypeError: open() got an unexpected keyword argument 'connect_kwargs'
不清楚如何修复,询问是否需要使用其他函数或替换为其他参数?
解决方案
Fabric的Connection.open()方法不接受connect_kwargs参数,可通过以下两种方式修复:
方法1:初始化Connection时传入认证参数
直接在创建Connection对象时传入connect_kwargs,且无需手动调用open()——run()方法会自动触发连接建立:
from fabric import Connection import socket uinaaaa = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) uinaaaa.connect(("8.8.8.8", 80)) xin = uinaaaa.getsockname()[0] # 初始化阶段传入认证参数 PERSONA = Connection(f"user@{xin}", connect_kwargs={'password': "LOSER"}) print("Ok, now you will learn all the server's commands!") YUIA = PERSONA.run("help") print(YUIA.stdout) # 原代码的standout是拼写错误,应为stdout print("If you want to learn more, type man, after that, a space, and then the command to learn more!")
方法2:调用open()时直接传入password参数
如果必须手动调用open(),可直接传入password参数(而非嵌套在connect_kwargs中):
PERSONA = Connection(f"user@{xin}") PERSONA.open(password="LOSER")
另外注意:原代码中的YUIA.standout是拼写错误,正确属性为YUIA.stdout,否则会触发新的属性错误。
内容的提问来源于stack exchange,提问作者user20576583
相关产品推荐
相关产品推荐

