尝试使用 Python PySFTP 模块连接到远程服务器时出现 WinError 10060 错误
WinError 10060 Error when trying to connect to remote server using Python PySFTP module
我正在尝试使用以下代码连接到远程服务器并列出路径中的文件:
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)
但是当我运行它时,它无法建立连接。它抛出以下异常:
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:\Users1841191\AppData\Roaming\Python\Python38\site-packages\pysftp\__init__.py", line 140, in __init__
self._start_transport(host, port)
File "C:\Users1841191\AppData\Roaming\Python\Python38\site-packages\pysftp\__init__.py", line 176, in _start_transport
self._transport = paramiko.Transport((host, port))
File "C:\Users1841191\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
任何人都可以帮助我解决为什么抛出此异常的原因,因为我尝试使用其他远程服务器并且代码似乎对他们来说工作正常?该代码仅针对一个特定的远程服务器抛出异常。
如果您的代码无法使用某种协议(在本例中为 SFTP)连接到某处,首先要测试的是,您是否可以使用来自同一台机器的任何现有 GUI/commandline 客户端使用相同的协议进行连接运行你的代码。
如果这也不起作用,则您没有编程问题,而是简单的网络连接问题。
如果您需要帮助解决问题,请转至 Super User or Server Fault。
我正在尝试使用以下代码连接到远程服务器并列出路径中的文件:
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)
但是当我运行它时,它无法建立连接。它抛出以下异常:
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:\Users1841191\AppData\Roaming\Python\Python38\site-packages\pysftp\__init__.py", line 140, in __init__
self._start_transport(host, port)
File "C:\Users1841191\AppData\Roaming\Python\Python38\site-packages\pysftp\__init__.py", line 176, in _start_transport
self._transport = paramiko.Transport((host, port))
File "C:\Users1841191\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
任何人都可以帮助我解决为什么抛出此异常的原因,因为我尝试使用其他远程服务器并且代码似乎对他们来说工作正常?该代码仅针对一个特定的远程服务器抛出异常。
如果您的代码无法使用某种协议(在本例中为 SFTP)连接到某处,首先要测试的是,您是否可以使用来自同一台机器的任何现有 GUI/commandline 客户端使用相同的协议进行连接运行你的代码。
如果这也不起作用,则您没有编程问题,而是简单的网络连接问题。
如果您需要帮助解决问题,请转至 Super User or Server Fault。