ssh paramiko可以读取xlsx文件

发布于 2025-01-30 03:30:17 字数 4253 浏览 3 评论 0原文

我将paramiko用于ssh

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )    
ssh.connect(Hostname, username=Username, password=Password)    
ftp = ssh.open_sftp()
files = ftp.listdir()
dir_oi = "directory_of_interest"
foi = ftp.listdir(dir_oi)

,可以找到csv成功地使用以下方式:

remote_file = ( dir_oi +"/" + foi[-1])
with ftp.open(remote_file) as f:
    df = pd.read_csv(f, sep = '\t', header = None)

我将remote_file更改为xlsx,然后

with ftp.open(uw_remote_file) as f:
    df = pd.read_excel(f)

我得到错误sshexception:服务器连接删除:套接字关闭

注释,我可以运行此行而无需任何错误ascoper_xlsx = ftp.open(uw_remote_file)

任何建议如何克服这一点?

按要求的logfile:

DEB [20220519-09:22:45.998] thr=1   paramiko.transport.sftp: [chan 0] listdir(b'blah')
DEB [20220519-09:22:48.009] thr=1   paramiko.transport.sftp: [chan 0] open(b'blah/halb.csv', 'r')
DEB [20220519-09:22:48.241] thr=1   paramiko.transport.sftp: [chan 0] open(b'blah/halb.csv', 'r') -> 35323935333939313533313032363062
DEB [20220519-09:22:49.084] thr=1   paramiko.transport.sftp: [chan 0] close(35323935333939313533313032363062)
DEB [20220519-09:23:24.790] thr=1   paramiko.transport.sftp: [chan 0] listdir(b'blah2')
DEB [20220519-09:24:01.590] thr=1   paramiko.transport.sftp: [chan 0] open(b'blah2/halb2.xlsx', 'r')
DEB [20220519-09:24:01.975] thr=1   paramiko.transport.sftp: [chan 0] open(b'blah2/halb2.xlsx', 'r') -> 37343338363564356234303033663337
DEB [20220519-09:24:23.510] thr=1   paramiko.transport.sftp: [chan 0] open(b'blah2/halb2.xlsx', 'r')
DEB [20220519-09:24:23.727] thr=1   paramiko.transport.sftp: [chan 0] open(b'blah2/halb2.xlsx', 'r') -> 64646361316532373233663463613036
DEB [20220519-09:24:24.108] thr=2   paramiko.transport: EOF in transport thread
DEB [20220519-09:24:24.108] thr=1   paramiko.transport.sftp: [chan 0] close(64646361316532373233663463613036)

trackback:

Traceback (most recent call last):
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\sftp_client.py", line 852, in _read_response
    t, data = self._read_packet()
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\sftp.py", line 201, in _read_packet
    x = self._read_all(4)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\sftp.py", line 188, in _read_all
    raise EOFError()
EOFError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\alexander.huhn.adm\AppData\Local\Temp\24\ipykernel_33560\4051829457.py", line 4, in <cell line: 2>
    df = pd.read_excel(f)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 457, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 1376, in __init__
    ext = inspect_excel_format(
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 1255, in inspect_excel_format
    buf = stream.read(PEEK_SIZE)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\file.py", line 219, in read
    new_data = self._read(read_size)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\sftp_file.py", line 185, in _read
    t, msg = self.sftp._request(
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\sftp_client.py", line 822, in _request
    return self._read_response(num)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\sftp_client.py", line 854, in _read_response
    raise SSHException("Server connection dropped: {}".format(e))
paramiko.ssh_exception.SSHException: Server connection dropped:

我可以下载,

filepath = uw_remote_file
localpath = "test.xlsx"
ftp.get(filepath,localpath)

因此可以下载该路线并在使用后删除

I use paramiko to ssh

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )    
ssh.connect(Hostname, username=Username, password=Password)    
ftp = ssh.open_sftp()
files = ftp.listdir()
dir_oi = "directory_of_interest"
foi = ftp.listdir(dir_oi)

and can find a read a csv successfully with:

remote_file = ( dir_oi +"/" + foi[-1])
with ftp.open(remote_file) as f:
    df = pd.read_csv(f, sep = '\t', header = None)

the minute I change remote_file to an xlsx, with

with ftp.open(uw_remote_file) as f:
    df = pd.read_excel(f)

I get the error SSHException: Server connection dropped: or Socket Closed

of note, I can run this line without any error existing_xlsx = ftp.open(uw_remote_file)

Any suggestions how to overcome this?

logfile as requested:

DEB [20220519-09:22:45.998] thr=1   paramiko.transport.sftp: [chan 0] listdir(b'blah')
DEB [20220519-09:22:48.009] thr=1   paramiko.transport.sftp: [chan 0] open(b'blah/halb.csv', 'r')
DEB [20220519-09:22:48.241] thr=1   paramiko.transport.sftp: [chan 0] open(b'blah/halb.csv', 'r') -> 35323935333939313533313032363062
DEB [20220519-09:22:49.084] thr=1   paramiko.transport.sftp: [chan 0] close(35323935333939313533313032363062)
DEB [20220519-09:23:24.790] thr=1   paramiko.transport.sftp: [chan 0] listdir(b'blah2')
DEB [20220519-09:24:01.590] thr=1   paramiko.transport.sftp: [chan 0] open(b'blah2/halb2.xlsx', 'r')
DEB [20220519-09:24:01.975] thr=1   paramiko.transport.sftp: [chan 0] open(b'blah2/halb2.xlsx', 'r') -> 37343338363564356234303033663337
DEB [20220519-09:24:23.510] thr=1   paramiko.transport.sftp: [chan 0] open(b'blah2/halb2.xlsx', 'r')
DEB [20220519-09:24:23.727] thr=1   paramiko.transport.sftp: [chan 0] open(b'blah2/halb2.xlsx', 'r') -> 64646361316532373233663463613036
DEB [20220519-09:24:24.108] thr=2   paramiko.transport: EOF in transport thread
DEB [20220519-09:24:24.108] thr=1   paramiko.transport.sftp: [chan 0] close(64646361316532373233663463613036)

traceback:

Traceback (most recent call last):
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\sftp_client.py", line 852, in _read_response
    t, data = self._read_packet()
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\sftp.py", line 201, in _read_packet
    x = self._read_all(4)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\sftp.py", line 188, in _read_all
    raise EOFError()
EOFError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\alexander.huhn.adm\AppData\Local\Temp\24\ipykernel_33560\4051829457.py", line 4, in <cell line: 2>
    df = pd.read_excel(f)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 457, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 1376, in __init__
    ext = inspect_excel_format(
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 1255, in inspect_excel_format
    buf = stream.read(PEEK_SIZE)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\file.py", line 219, in read
    new_data = self._read(read_size)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\sftp_file.py", line 185, in _read
    t, msg = self.sftp._request(
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\sftp_client.py", line 822, in _request
    return self._read_response(num)
  File "C:\Users\alexander.huhn.adm\Anaconda3\lib\site-packages\paramiko\sftp_client.py", line 854, in _read_response
    raise SSHException("Server connection dropped: {}".format(e))
paramiko.ssh_exception.SSHException: Server connection dropped:

I can download using

filepath = uw_remote_file
localpath = "test.xlsx"
ftp.get(filepath,localpath)

so will go down that route and delete after use

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒中人 2025-02-06 03:30:17

从日志中,看起来它可以打开两个文件。 pandas正在陷入EOF错误,因此,这意味着Excel文件是完全空的。

您可以确认是否为空。

From the log it looks like it were able to open both files. pandas is running into EOF error, So, which means excel file is completely empty.

Can you confirm if that is empty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文