动易网站管理系统,营销型网站制作服务商,海口海南网站建设,设计师网络用语应用场景#xff1a;从国内往国外上传#xff0c;因国际带宽影响#xff0c;速度很慢#xff0c;于是做了一个中转FTP#xff0c;而自动上传需求也就诞生了。代码地址#xff1a;https://github.com/coocla/linux/blob/master/ftp/autoupload_ftp.py sftp类型#xff1… 应用场景从国内往国外上传因国际带宽影响速度很慢于是做了一个中转FTP而自动上传需求也就诞生了。代码地址https://github.com/coocla/linux/blob/master/ftp/autoupload_ftp.py sftp类型https://github.com/coocla/linux/blob/master/sftp/autoupload_sftp.py直接贴脚本#!/usr/bin/env python
# -*- coding:utf-8 -*-
# This is a new file will be automatically uploaded to the ftp python script.
# Author: http://blog.coocla.orgimport pyinotify
import os
from ftplib import FTPWATCHDIR/tmp/
HOSTxxxxx
USERxxxxx
PASSxxxxx
pid_file/tmp/monitor_upload.pid
log_file/tmp/monitor_upload.logclass OnCreateClick(pyinotify.ProcessEvent):def process_IN_CREATE(self, event):abspathos.path.join(event.path, event.name)if os.path.isdir(abspath):print create directory: %s % event.nameelif os.path.isfile(abspath):print create file: %s % event.namerelnameabspath.split(WATCHDIR)[1]relpathos.path.dirname(relname)upload(abspath, relpath, event.name)def main():wm pyinotify.WatchManager()notifier pyinotify.Notifier(wm, OnCreateClick())wm.add_watch(WATCHDIR, pyinotify.IN_CREATE, recTrue, auto_addTrue)notifier.loop(daemonizeTrue, pid_filepid_file, stdoutlog_file)def upload(abspath, relpath, filename):ftp FTP()ftp.connect(HOST)ftp.login(USER, PASS)bufsize 1024ftp.cwd(xxxxxxx)try:ftp.cwd(relpath)except:for remote_path in relpath.split(os.path.sep):ftp.mkd(remote_path)ftp.cwd(remote_path)filehandler open(abspath, rb)try:ftp.storbinary(STOR filename, filehandler, bufsize)print Upload file %s success. % abspathexcept:print Upload file %s error. % abspathfilehandler.close()ftp.quit()if __name__ __main__:if os.path.isfile(pid_file):os.remove(pid_file)main() 转载于:https://blog.51cto.com/siliotto/1598148