简体   繁体   English

WinError 10060 尝试使用 Python PySFTP 模块连接到远程服务器时出错

[英]WinError 10060 Error when trying to connect to remote server using Python PySFTP module

I am trying to connect to a remote server and list files inside a path using the below code:我正在尝试使用以下代码连接到远程服务器并列出路径内的文件:

import pysftp

myHostname = "myhostname.com"
myPort = <someportnumber>
myUsername = "<valid username>"
myPassword = "<valid password>"

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts= cnopts, port=myPort) as sftp:
    print("Connection succesfully established ... ")

    sftp.chdir('/logs/dev')

    # Obtain structure of the remote directory
    directory_structure = sftp.listdir_attr()

    # Print data
    for attr in directory_structure:
        print(attr.filename, attr)

But when I am running it, It is unable to establish a connection.但是当我运行它时,它无法建立连接。 It's throwing below exception:它抛出以下异常:

Traceback (most recent call last):
  File "c:/Users/611841191/Documents/SFTP File Download/SFTPFileDownload.py", line 11, in <module>
    with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts= cnopts, port=myPort) as sftp:
  File "C:\Users\611841191\AppData\Roaming\Python\Python38\site-packages\pysftp\__init__.py", line 140, in __init__
    self._start_transport(host, port)
  File "C:\Users\611841191\AppData\Roaming\Python\Python38\site-packages\pysftp\__init__.py", line 176, in _start_transport
    self._transport = paramiko.Transport((host, port))
  File "C:\Users\611841191\AppData\Roaming\Python\Python38\site-packages\paramiko\transport.py", line 415, in __init__
    raise SSHException(
paramiko.ssh_exception.SSHException: Unable to connect to <myhostname.com>: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond 

Can anyone help me out with why this exception is being thrown because I tried with other remote servers and the code seems to be working fine for them?任何人都可以帮我解释为什么抛出这个异常,因为我尝试使用其他远程服务器并且代码似乎对他们来说工作正常? The code is throwing exception for one particular remote server only.该代码仅对一个特定的远程服务器抛出异常。

If your code cannot connect somewhere using some protocol (SFTP in this case), the first thing to test is, whether you can connect using the same protocol using any existing GUI/commandline client from the same machine that runs your code.如果您的代码无法使用某种协议(在这种情况下为 SFTP)连接到某个地方,那么首先要测试的是,您是否可以使用运行代码的同一台机器上的任何现有 GUI/命令行客户端使用相同的协议进行连接。

If that does not work either, you do not have a programming question, but a simple network connectivity problem.如果这也不起作用,那么您没有编程问题,而是一个简单的网络连接问题。

If you need a help resolving the problem, please go to Super User or Server Fault .如果您需要帮助解决问题,请将 go 发送至超级用户服务器故障

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM