【问题标题】:Installing Python's Cryptography on Windows在 Windows 上安装 Python 的密码学
【发布时间】:2017-11-22 13:15:29
【问题描述】:

我在 Windows 上创建了一个脚本来连接到远程 SSH 服务器。我已经成功安装了cryptographypynacl,最后是paramiko(花了我一整天的时间来弄清楚如何在 Windows 上成功安装它们)。

现在我运行脚本,它会弹出一个错误,指出 DLL 加载失败。该错误似乎与libsodium 有关,但我无法准确确定要尝试加载的DLL 以及从何处加载。为了安全起见,我还安装了pysodium

这是脚本:

automate.py

import SSH

connection = ssh("10.10.65.100", "gerrit2", "gerrit@123")
print("Calling OpenShell")
connection.openShell()
print("Calling sendShell")
connection.sendShell("ls -l")
print("Calling process")
connection.process()
print("Calling closeConnection")
connection.closeConnection()

SSH.py

import threading, paramiko

class ssh:
    shell = None
    client = None
    transport = None

    def __init__(self, address, username, password):
        print("Connecting to server on ip", str(address) + ".")
        self.client = paramiko.client.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
        self.client.connect(address, username=username, password=password, look_for_keys=False)
        self.transport = paramiko.Transport((address, 22))
        self.transport.connect(username=username, password=password)

        thread = threading.Thread(target=self.process)
        thread.daemon = True
        thread.start()

    def closeConnection(self):
        if(self.client != None):
            self.client.close()
            self.transport.close()

    def openShell(self):
        self.shell = self.client.invoke_shell()

    def sendShell(self, command):
        if(self.shell):
            self.shell.send(command + "\n")
        else:
            print("Shell not opened.")

    def process(self):
        global connection
        while True:
            # Print data when available
            if self.shell != None and self.shell.recv_ready():
                alldata = self.shell.recv(1024)
                while self.shell.recv_ready():
                    alldata += self.shell.recv(1024)
                strdata = str(alldata, "utf8")
                strdata.replace('\r', '')
                print(strdata, end = "")
                if(strdata.endswith("$ ")):
                    print("\n$ ", end = "")

这是错误:

> python automate.py

Traceback (most recent call last):
  File "automate.py", line 1, in <module>
    import SSH
  File "D:\Automate\SSH_Paramiko\SSH.py", line 1, in <module>
    import threading, paramiko
  File "D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\lib\site-packages\paramiko-2.4.0-py3.7.egg\paramiko\__init__.py", line 22, in <module>
  File "D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\lib\site-packages\paramiko-2.4.0-py3.7.egg\paramiko\transport.py", line 57, in <module>
  File "D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\lib\site-packages\paramiko-2.4.0-py3.7.egg\paramiko\ed25519key.py", line 22, in <module>
  File "D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\lib\site-packages\nacl\signing.py", line 19, in <module>
    import nacl.bindings
  File "D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\lib\site-packages\nacl\bindings\__init__.py", line 17, in <module>
    from nacl.bindings.crypto_box import (
  File "D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\lib\site-packages\nacl\bindings\crypto_box.py", line 18, in <module>
    from nacl._sodium import ffi, lib
ImportError: DLL load failed: The specified module could not be found.

【问题讨论】:

  • 好像 pynacl 没有安装好。无法找到其扩展模块之一(或其某些 .dll 依赖项)。您应该能够通过以下方式重现该问题:import paramiko。顺便说一句:你的路径中的 Python37 是什么?
  • @CristiFati : 我做了where python 并收到了这条路径D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\python.exe
  • @CristiFati:谢谢你给我的线索。费了好大劲,终于解决了这个问题。

标签: python windows ssh


【解决方案1】:

经过大量谷歌搜索,我终于偶然发现了this。正如对话中提到的,我卸载了我之前的 pynacl 安装,从 https://github.com/lmctv/pynacl/archive/v1.2.a0.reorder.zip 下载了压缩源,从 https://github.com/jedisct1/libsodium/releases/download/1.0.15/libsodium-1.0.15.tar.gz 下载了 libsodium,将 LIB 环境变量设置为 D:\Users\prashant-gu\Downloads\libsodium-1.0.15\bin\x64\Release\v140\dynamic,最后使用

pip install .

现在可以正常使用了。

在安装paramiko 的过程中,我还碰巧从https://ci.cryptography.io/job/cryptography-support-jobs/job/openssl-release-1.1/ 下载了OpenSSL,并将INCLUDE 环境变量设置为D:\Users\prashant-gu\Downloads\openssl-1.1.0g-2015-x86_64\openssl-win64-2015\include,以便成功安装cryptography 包,该包恰好是@987654331 的依赖项@。

【讨论】:

    猜你喜欢
    • 2017-12-18
    • 2022-01-21
    • 2014-11-12
    • 2016-05-10
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    相关资源
    最近更新 更多