在本地缓存和sftp服务器之间同步文件

sftpsyncer的Python项目详细描述


在本地缓存和远程缓存之间逐段同步文件 SFTP服务器。

用例

您的客户机有时需要将文件同步到和 从SFTP服务器。

  • open()一个文件,不管它是远程的还是本地的。 *如果文件在本地可用,请使用本地副本。 *如果本地文件不存在,则下载它,然后使用本地副本。
  • 将()文件添加到上载包,然后push()上载所有本地排队的文件 文件夹。

这个模型很简单,如果 存在同名的文件。如果你想避免,你可能会更好 不用git之类的。

使用方法:

import sftpsyncer
from sftpsyncer import syncagent

sagent = syncagent(hostname="remote_sagenth.example.com", host_port="1222",
                username="theuser", key_path="/home/theuser/.sagenth/key",
                local_root="/tmp/cache", remote_root="/upload_target")

# upload local files

sagent.upload_queue.add("myfile.txt")
sagent.upload_queue.add("docs/readme.txt")

sagent.push()  # upload whether or not remote files exist

# auto-get remote files, reading from cache when available

fh1 = sagent.open("myfile.txt")  # file is already local, return local fh
fh2 = sagent.open("remote.txt")  # file is not local, transfer to local cache, return local fh

# exceptions

try:
    fh3 = sagent.open("not_anywhere.txt")
except sftpsyncer.exceptions.FileNotFound as e:
    print "File does not exist on local or remote"

try:
    fh5 = sagent.open("ssh_server_unavailable.txt")
except sftpsyncer.exceptions.ConnectionError as e:
    print "Remote SFTP service appears to be unavailable"

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
用Java将dpi元数据写入jpeg图像   文件为什么Java会自动解码URI编码的文件名中的%2F?   java多动态时间事件侦听器   java中如何递归查找不同级别的属性值   java Apache Spark MySQL JavaRDD。foreachPartition为什么我得到ClassNotFoundException   Java中的循环变量文件名   java如何参数化调用JNA库的C库名称和路径?   swing如何使gif在Java中运行一次并停止?   Big Sur上的MacOS应用程序,具有java子进程和可访问性API权限   在page类中定义的java Webelement在测试类中不可访问   如何使用java if else从JSON文件中删除重复的元素?   java Android studio dalvik vm找不到类   java在数组中正确使用协方差