Skip to main content

transfer file using ftp, scp or sftp

Project description

transferfile

介绍

sftp、ftp、scp、rsync 传输文件的统一接口,无论哪一种传输方式,都可以适配。

程序适用于数据服务系统,如数据仓库对下游系统分发数据文件。

安装

pip install transferfile

使用说明

采用简单工厂模式,传入的 type 有:

  • ftp
  • sftp
  • scp
  • rsync
ftp

请注意原路径和目标路径都是带文件名的路径

传输 /path/to/src.txt 到 /path/to/dist.txt

from transferfile import TransferFactory
client = TransferFactory.create(type = "ftp",host = "172.17.0.2",username = "admin",password= "admin")
client.put("/path/to/src.txt","/path/to/dist.txt")

如果目标路径 /path/to/ 不存在,会递归创建。

如果程序运行在 linux 环境(utf8编码),而 ftp 服务器在 Windows 环境(GBK编码),为了文件名看起来不是乱码,可以指定服务端的编码方式为 GBK,指定特殊端口也是可以的,默认是 21

from transferfile import TransferFactory
client = TransferFactory.create(type = "ftp",host = "172.17.0.2",port = 21,username = "admin",password= "admin",encoding = 'gbk')
client.put("/path/to/src.txt","/path/to/dist.txt")

下载文件使用 get,下载远程 /path/to/remote.txt 到本地 /path/to/local.txt:

from transferfile import TransferFactory
client = TransferFactory.create(type = "ftp",host = "172.17.0.2",port = 21,username = "admin",password= "admin",encoding = 'gbk')
client.get("/path/to/local.txt","/path/to/remote.txt")
sftp,scp

请注意原路径和目标路径都是带文件名路径

用 scp 或 sftp 时只需要修改 type 参数为 scp 或 sftp 即可:

from transferfile import TransferFactory
client = TransferFactory.create(type = "sftp",host = "172.17.0.2",username = "admin",password= "admin")
client.put("/path/to/src.txt","/path/to/dist.txt")

也可以指定 rsa_私钥文件,从而不需要密码

from transferfile import TransferFactory
client = TransferFactory.create(type = "scp",host = "172.17.0.2",username = "admin", rsa_file = "/root/.ssh/id_rsa")
client.put("/path/to/src.txt","/path/to/dist.txt")

如果已经做好了 ssh 的授信,可以传入参数 load_system_host_keys = True,从而不需要密码

from transferfile import TransferFactory
client = TransferFactory.create(type = "scp",host = "172.17.0.2",username = "admin", load_system_host_keys = True )
client.put("/path/to/src.txt","/path/to/dist.txt")
rsync

请注意原路径和目标路径可以是文件名,也可以是目录

rsync 目前需要配置好授信才可以使用,暂时不支持用户名和密码登录。

from transferfile import TransferFactory
client = TransferFactory.create(type = "rsync",host = "172.17.0.2",username = "admin", load_system_host_keys = True)
client.put("/path/to/src.txt","/path/to/dist.txt")
扩展点

由于 ftp 不支持传输目录,这里就没有做出传输整个目录的接口,要实现也不复杂,如果要实现就是递归读取目录的文件,然后一个一个传输。也可以压缩成一个文件后上传。

目前工作中暂时没有这个需求,都是生成好一个文件就传输这个文件,不需要上传整个目录。因此就没有实现。

更新日志

0.7: sftp 增加递归创建目标路径功能 。

0.6: ftp 增加 windows 类型目录的支持。

0.5: ftp 增加链接类型目录的支持。

ftp 创建目录前会判定目录是否存在,如果不存在就递归创建目录。增加链接型的目录的支持。

例如 /data_file -> /home/user/data_file

之前目标路径必须是 /home/user/data_file/path/dist.dat

0.5 版本之后可以是 /data_file/path/dist.dat

联系我

有问题,欢迎交流讨论。

公众号 「Python七号」,分享 Python 编程技能。

个人微信 「somenzz」

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

transferfile-0.7.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

transferfile-0.7-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file transferfile-0.7.tar.gz.

File metadata

  • Download URL: transferfile-0.7.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.5

File hashes

Hashes for transferfile-0.7.tar.gz
Algorithm Hash digest
SHA256 7f6f154d929781fbc3ea6b66c91597fa22f77de215330f62562fe7f4701a83fe
MD5 6f313de08a209024360594257cc8a534
BLAKE2b-256 71a23b3bf4e9ec3b366f992218cd052647518dc5ccf515a603dd4dbd8e5ed81c

See more details on using hashes here.

File details

Details for the file transferfile-0.7-py3-none-any.whl.

File metadata

  • Download URL: transferfile-0.7-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.5

File hashes

Hashes for transferfile-0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a5f27f6490fb8e860bd88485ab18b213a9e06f797f9b7f8961afeb2b7376492c
MD5 7f25cb5962e6619b0b0aabe09e713bd6
BLAKE2b-256 d29854e9c692c8796fe9272c4af7f5099c9965bc2ad896018dad2d04f76dc2eb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page