得到ValueError:发送带有paramiko和flask socketio的文件时读取关闭的文件

问题描述

当尝试使用paramiko sftp将文件发送到ssh服务器时,出现ValueError:读取已关闭文件 note:在HTTP视图中尝试执行相同操作时,此功能非常完美

这是我连接到服务器并发送文件的方式 而且我认为send_filestorage(FILE,FILE.filename,path)是导致错误的行

@socketio.on('client-connect')
def client_connected(message):
    global data
    
    HOST = data["host"]
    HOSTUSERNAME = data['username']
    PASSWORD = data['password']
    PATH = data['path']
    FILE = data['file']
    if data:
        for (host,hostusername,password,path) in zip(HOST,HOSTUSERNAME,PASSWORD,PATH):

            #connect to the ssh server
            emit('server_try_connect',{'data' : str('trying to connect to server '+host)})
            client = Connection(host=host,username=hostusername,password=password)
            emit('server-connected',{'data' :  'client connected'})
            emit('server-connected-establish',{'data' : str('establishing connection to  '+host)})
            client.connect()
            emit('server-connected-success',{'data' :  'client connected'})
            #sending files.....
            emit('sending-files',{'data' : "sending file"})

             ##########
             #this is the line that cause the error
            client.send_filestorage(FILE,FILE.filename,path)
         ############

            emit('files-sent',{'data' : "file sent successfully"})
            emit('files-extract',{'data' : "extrcting files"})
            emit('files-extract-success',{'data' : str(client.extract(path,file_name=FILE.filename))})
        
            #check if the file existe / and command lines
            client.excute_command("ls")
            emit('list-dir',{'data' : str(client.check_command())})
            client.close_connection()
            emit('connection-closed',{'data' :  ('connection closed')})

这是send_filestorage(FILE,FILE.filename,path)函数
错误是由于以下原因引起的:sftp.putfo(file_storage,file_in_server) file_in_server:是我要发送此文件的路径

 #send a storage file
    def send_filestorage(self,file_storage,file_in_server,file_path_inserver):
        sftp = self.client.open_sftp()

        # create a file path if the we have a path from the user
        if file_path_inserver:
            self.create_and_navigate_topath(sftp,file_path_inserver)
        sftp.putfo(file_storage,file_in_server)
        sftp.close()

这是错误

Exception in thread Thread-7:
Traceback (most recent call last):
  File "c:\users\icom\appdata\local\programs\python\python37\Lib\threading.py",line 926,in _bootstrap_inner
    self.run()
  File "c:\users\icom\appdata\local\programs\python\python37\Lib\threading.py",line 870,in run
    self._target(*self._args,**self._kwargs)
  File "C:\Users\icom\Envs\wisevenv\lib\site-packages\socketio\server.py",line 682,in _handle_event_internal
    r = server._trigger_event(data[0],namespace,sid,*data[1:])
  File "C:\Users\icom\Envs\wisevenv\lib\site-packages\socketio\server.py",line 711,in _trigger_event
    return self.handlers[namespace][event](*args)
  File "C:\Users\icom\Envs\wisevenv\lib\site-packages\flask_socketio\__init__.py",line 283,in _handler
    *args)
  File "C:\Users\icom\Envs\wisevenv\lib\site-packages\flask_socketio\__init__.py",line 713,in _handle_event
    ret = handler(*args)
  File "C:\Users\icom\myProjects\share-wise\app.py",line 51,in test
    client.send_filestorage(FILE,path)
  File "C:\Users\icom\myProjects\share-wise\utils.py",in send_filestorage
    sftp.putfo(file_storage,file_in_server)
  File "C:\Users\icom\Envs\wisevenv\lib\site-packages\paramiko\sftp_client.py",line 717,in putfo
    reader=fl,writer=fr,file_size=file_size,callback=callback
  File "C:\Users\icom\Envs\wisevenv\lib\site-packages\paramiko\sftp_client.py",line 678,in _transfer_with_callback
    data = reader.read(32768)
  File "C:\Users\icom\Envs\wisevenv\lib\tempfile.py",line 736,in read
    return self._file.read(*args)
  File "C:\Users\icom\Envs\wisevenv\lib\tempfile.py",line 481,in func_wrapper
    return func(*args,**kwargs)
ValueError: read of closed file

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)