在Windows上与Pyinstaller打包时,Paramiko SSHClient.connect(...)失败

时间:2019-07-15 22:02:36

标签: python windows pyinstaller paramiko

Paramiko的SSHClient.connect(...)方法与Pyinstaller打包时会引发异常,但在未打包时会运行良好。

使用两个Pyinstaller标志--onedir--onefile都会发生这种情况。我已经尝试过Pyinstaller版本3.5和当前最新的开发版本4.0.dev0+ce887b462--noupx效果不明显。

我的Paramiko版本是2.6.0

我当前的Python版本是2.7.15

我的操作系统是Windows 10 Pro,版本10.0.17134,内部版本17134。

此问题似乎是由于Paramiko在kernel32.dll中调用C函数导致的,该函数仅在打包后意外返回null

最小示例:

import paramiko

# The nature of this issue unfortunately requires
# a server that responds to SSH connections to test
host = None  # Put a valid hostname here
port = 22  # Put a valid port here

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
    hostname=host,
    port=port,
    username=None,
    password=None,
    timeout=5
)

我希望Pyinstaller打包的版本与未打包的版本完全相同-在这种情况下,启动SSH连接,然后由于AuthenticationException而失败,因为usernamepasswordNone

> virtualenv venv
> venv\Scripts\activate.bat
> pip install pyinstaller==3.5 paramiko==2.6.0
[output truncated for brevity]
> pyinstaller minimal-paramiko-example.py
[output truncated for brevity]
> dist\minimal-paramiko-example\minimal-paramiko-example.exe
Traceback (most recent call last):
  File "minimal-paramiko-example.py", line 16, in <module>
    timeout=5
  File "site-packages\paramiko\client.py", line 446, in connect
  File "site-packages\paramiko\client.py", line 691, in _auth
  File "site-packages\paramiko\agent.py", line 379, in __init__
  File "site-packages\paramiko\agent.py", line 65, in _connect
  File "site-packages\paramiko\agent.py", line 82, in _send_message
  File "site-packages\paramiko\win_pageant.py", line 129, in send
  File "site-packages\paramiko\win_pageant.py", line 96, in _query_pageant
  File "site-packages\paramiko\_winapi.py", line 179, in write
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
[16768] Failed to execute script minimal-paramiko-example

> python minimal-paramiko-example.py  # Will result in an AuthenticationException, since username and password are None; this is expected and normal
[output truncated for brevity]

1 个答案:

答案 0 :(得分:0)

此问题是由与PuTTY Pageant的交互引起的。关闭PuTTY Pageant会导致此错误不会发生。由于某些原因,它仅在以下情况下出现:

  • 使用Pyinstaller
  • 正在运行的PuTTY选美比赛

不能同时满足这两个要求。