尝试使用Python到SFTP文件时定义传输模式 [英] Define transfer mode when trying to SFTP files using Python

查看:335
本文介绍了尝试使用Python到SFTP文件时定义传输模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正尝试使用python脚本(驻留在SFTP服务器上)将文本文件从Linux服务器传输到Windows服务器.

We are trying to transfer text files from a Linux server to a Windows server using a python script (which resides on the SFTP server).

我们有必要确保使用文本模式传输文件.我在pysftp中看不到这种可能性.还有其他支持此功能的Python库吗?

It is necessary for us to ensure the files are transferred using text mode. I don't see this possibility in pysftp. Is there any other Python library that supports this?

推荐答案

pysftp/Paramiko使用SFTP协议版本3.

pysftp/Paramiko uses an SFTP protocol version 3.

在SFTP协议版本3中,没有传输模式.换句话说,只有 binary 传输模式.

In the SFTP protocol version 3, there are no transfer modes. Or in other words, there is only the binary transfer mode.

即使pysftp/Paramiko支持较新版本的SFTP(它确实支持文本/ASCII模式),也不太可能为您提供帮助.大多数SFTP服务器都是OpenSSH.而且OpenSSH也使用SFTP 3.

Even if pysftp/Paramiko supported a newer version of SFTP, which do support the text/ascii mode, it is unlikely to help you. Most SFTP servers are OpenSSH. And OpenSSH uses SFTP 3 too.

另请参见如何在SFTP中传输二进制文件?

如果需要将文件转换为Windows格式,则需要在文件传输之前先进行处理.

If you need to convert the file to Windows format, you need to do it upfront, before a file transfer.

一个简单的实现就像:

WINDOWS_LINE_ENDING = b'\r\n'
UNIX_LINE_ENDING = b'\n'

with open("/local/path/file.txt", "rb") as local_file:
    contents = local_file.read()
    contents = contents.replace(UNIX_LINE_ENDING, WINDOWS_LINE_ENDING)
    with sftp.open("/remote/path/file.txt", "wb") as remote_file:
        remote_file.write(contents)

(基于如何在Windows计算机上使用Python将CRLF转换为LF )

这篇关于尝试使用Python到SFTP文件时定义传输模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆