sftp HTTP

Recently a demand synchronization of data, need to archive automatically uploaded to the VPS.

Based on security considerations, you can not use FTP, SFTP protocol can only walk. And taking into account the problem of network cards off, you must also have the functions of HTTP.

With python operation sftp, the natural choice is paramiko library. FTP operation just before the operation is similar. But what can be difficult to live off of my resume, because the file has been playing the operation, which is open, then read / write, the last close.

What's SFTP off resume, Chenqie do ah.

Stackoverflow can only help, and a people pointing to me, can be used to calculate the size of the uploaded file, and then fill in the missing. Principle we all know, this is the key to Daren told me SFTP function corresponding.

Then write the following code:

# -*- coding:utf-8 -*-
import config
import paramiko
import os
privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
transport = paramiko.Transport((config.host, config.port))
transport.connect(username=config.username, pkey=mykey)

sftp = paramiko.SFTPClient.from_transport(transport)

remot_dir = 'xxx/xxx/xxx'
local_dir = 'xxx/xxx/xxx'
filename = 'xxx.tar'
file_list = sftp.listdir(remot_dir)

if filename in file_list:
    stat = sftp.stat(remot_dir + filename)
    f_local = open(local_dir + filename)
    f_local.seek(stat.st_size)
    f_remote = sftp.open(remot_dir + filename, "a")
    tmp_buffer = f_local.read(100000)
    while tmp_buffer:
        f_remote.write(tmp_buffer)
        tmp_buffer = f_local.read(100000)
    f_remote.close()
    f_local.close()
else:
    f_local = open(local_dir + filename)
    f_remote = sftp.open(remot_dir + filename, "w")
    tmp_buffer = f_local.read(100000)
    while tmp_buffer:
        f_remote.write(tmp_buffer)
        tmp_buffer = f_local.read(100000)
    f_remote.close()
    f_local.close()

sftp.close()
transport.close()

I test it, and then manually broken network links, compressed file uploaded properly play. Do not trust students can also put forward under MD5 checksum.

Reproduced in: https: //www.jianshu.com/p/0dc3810b4430

Guess you like

Origin blog.csdn.net/weixin_34281477/article/details/91214601