【问题标题】:Python Flask: Connect function unable to handle variables in hostnamePython Flask:连接函数无法处理主机名中的变量
【发布时间】:2020-01-28 18:52:17
【问题描述】:

我正在使用 Python Flask 库并尝试连接到主机。 问题是,如果我将主机名存储在变量中并作为参数传递,它会失败并出现错误。

如何传递变量?

uname = request.args.get('username')
pwd = request.args.get('password')
client = request.args.get('client')

print("username entered = ", uname)
print("password entered = ", pwd)
print("FAT client = ", client)

print("Initiating connection to ", client, " and triggering SPU")
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(client, username=uname, password=pwd, timeout=900) # This errors as client is a variable. If i hardcode client to a real hostname it works
paramiko.util.log_to_file("filename.log")

简要错误:

TypeError: getaddrinfo() argument 1 must be string or None

错误堆栈跟踪是:

Traceback (most recent call last):
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\dev\HelloWorld\Flask_POST_PUT_test.py", line 24, in login_page
    client.connect(client, username=uname, password=pwd, timeout=900)
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\paramiko\client.py", line 340, in connect
    to_try = list(self._families_and_addresses(hostname, port))
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\paramiko\client.py", line 203, in _families_and_addresses
    addrinfos = socket.getaddrinfo(
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
TypeError: getaddrinfo() argument 1 must be string or None

【问题讨论】:

  • 我看到了一个相关的答案:@Martin Prikryl 的stackoverflow.com/questions/58683322/…,但即使我使用client.connect(hostname=client,它也不起作用。
  • @Martin Prikryl 有什么线索吗?

标签: python flask


【解决方案1】:

paramiko.client.SSHClient.connect的第一个参数应该是hostname,是一个字符串。您正在传递 paramiko.SSHClient 本身。您使用client = request.args.get('client') 正确获取了主机名,但随后使用client = paramiko.SSHClient() 覆盖了它。 为您的主机名和 sshclient 使用不同的变量名称,如下所示。

uname = request.args.get('username')
pwd = request.args.get('password')
client = request.args.get('client')

print("username entered = ", uname)
print("password entered = ", pwd)
print("FAT client = ", client)

print("Initiating connection to ", client, " and triggering SPU")
sshclient = paramiko.SSHClient()
sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshclient.connect(client, username=uname, password=pwd, timeout=900)
paramiko.util.log_to_file("filename.log")

【讨论】:

  • 非常感谢 - 我犯了一个多么愚蠢的错误。很好的收获。
猜你喜欢
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
相关资源
最近更新 更多