Объект 'NoneType' не имеет атрибута 'open_session - PullRequest
0 голосов
/ 22 мая 2019

Я написал скрипт для подключения к серверу sftp в python, но он показывает эту ошибку ниже, и я не понимаю его.пожалуйста, помогите мне исправить ошибку

import pysftp

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

with pysftp.Connection(host="127.0.0.1", username="new34",password="password",cnopts=cnopts) as srv:
    print("connection successful")

# Get the directory and file listing
data = srv.listdir()

srv.put("testfile.txt")


# Closes the connection
srv.close()

# Prints out the directories and files, line by line
for i in data:
    print(i)

она показывает следующую ошибку;помогите пожалуйста исправить ошибку

C:\Users\Rohan\PycharmProjects\untitled1\venv\Scripts\python.exe C:/Users/Rohan/PycharmProjects/untitled1/yu.py
C:\Users\Rohan\PycharmProjects\untitled1\venv\lib\site-packages\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from C:\Users\Rohan\.ssh\known_hosts.  You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
  warnings.warn(wmsg, UserWarning)
Traceback (most recent call last):
connection successful
  File "C:/Users/Rohan/PycharmProjects/untitled1/yu.py", line 10, in <module>
    data = srv.listdir()
  File "C:\Users\Rohan\PycharmProjects\untitled1\venv\lib\site-packages\pysftp\__init__.py", line 591, in listdir
    self._sftp_connect()
  File "C:\Users\Rohan\PycharmProjects\untitled1\venv\lib\site-packages\pysftp\__init__.py", line 205, in _sftp_connect
    self._sftp = paramiko.SFTPClient.from_transport(self._transport)
  File "C:\Users\Rohan\PycharmProjects\untitled1\venv\lib\site-packages\paramiko\sftp_client.py", line 164, in from_transport
    chan = t.open_session(
AttributeError: 'NoneType' object has no attribute 'open_session'

Process finished with exit code 1

1 Ответ

0 голосов
/ 22 мая 2019

Ваш код имеет проблему с отступом. Попробуйте это,

with pysftp.Connection(host="127.0.0.1", username="new34",password="password",cnopts=cnopts) as srv:
    print("connection successful")
    # Get the directory and file listing
    data = srv.listdir()

    srv.put("testfile.txt")

with автоматически закрывает соединение. Не нужно закрывать явно.

...