Python3 write a simple local file upload server instance

Source: Internet
Author: User
This article mainly introduces about Python3 write simple local file Upload server instance, has a certain reference value, now share to everyone, have the need of friends can refer to

Python is a fun thing to do? Well I casually said, anyway because of various reasons (in fact, what I do not know), a simple study of the next Python, and then wrote an upload file on the server's small toy practiced hand.

The approximate function is this:

1. Get a list of local files (including folders)

2, check the existence of the server, there is no direct upload, there is, folder disregard, file comparison size, size inconsistency is overwritten, and finally check the server on the existence of the local does not have a file, the existence is deleted

3, after the addition of the ignore list, ignoring the file type

4. Then added reboot Tomcat, but this feature is not tested

That's probably the way it is, oh, lost code.

#!/usr/bin/env python3#-*-coding:utf-8-*-import os import os.path import paramikoimport datetimeimport re# Configuration Property Config = {#本地项目路径 ' Local_path ': ', # Server project path ' Ssh_path ': ', # project name ' project_name ': ', # Ignore list ' ignore_list ': [],# ssh address, port, username, password ' h Ostname ': ', ' Port ': ', ' username ': ', ' Password ': ', # Whether to force update ' mandatory_update ': Restart Tomcat after the false,# update is complete restart_tom Cat ': false,# Tomcat bin address ' tomcat_path ': ', # ignored file type ' ignore_file_type_list ': []}# checks if folder exists, does not exist then creates Def check_folder (p ATH): stdin, stdout, stderr = Ssh.exec_command (' find ' + path) result = Stdout.read (). Decode (' Utf-8 ') if len (result) = = 0:p r Int (' directory%s does not exist, create directory '% path ') ssh.exec_command (' mkdir ' + path) print ('%s ' creation succeeded '% path ') return 1else:print (' directory%s already exists '% path ') Return 0# Check whether the file exists, there is no direct upload, there is a check size is the same, not the same upload def check_file (Local_path, Ssh_path): # Check whether the file exists, there is no direct upload stdin, stdout, stderr = Ssh.exec_command (' find ' + ssh_path) result = Stdout.read (). Decode (' Utf-8 ') if len (result) = = 0:sftp.put (local_pa Th,ssh_path) print ('%s upload succeeded '% (SSH_path)) return 1else:# exists then compare file size # local file Size lf_size = Os.path.getsize (local_path) # target file size stdin, stdout, stderr = ssh.exec_ Command (' du-b ' + ssh_path) result = Stdout.read (). Decode (' utf-8 ') tf_size = Int (result.split (' \ t ') [0]) print (' Local file size:% s, the remote file size is:%s '% (Lf_size, tf_size)) if lf_size = = Tf_size:print ('%s size is the same as local file, do not update '% (Ssh_path)) ' Return 0else:sftp.put ( Local_path,ssh_path) print ('%s update success '% (Ssh_path)) return # upload process start print (' upload start ') begin = Datetime.datetime.now () # Folder List folder_list = []# file list file_list = []# ssh file list ssh_file_list = []for parent,dirnames,filenames in Os.walk (config[' loc Al_path ']+config[' project_name '): #初始化文件夹列表 for dirname in dirnames:p = Os.path.join (parent,dirname) folder_ List.append (P[p.find (config[' project_name '):]) #初始化文件列表 for filename in filenames:if config[' ignore_list '].count ( filename) = = 0:p = Os.path.join (parent,filename) file_list.append (p[p.find (config[' project_name '):]) Print (' Total folder% ' s, file%s '% (len (folder_list), Len (file_list))) # SSH Console ssh = Paramiko. SshcliENT () ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Ssh.connect (hostname=config[' hostname ', port=config[' Port '], username=config[' username '), password=config[' password ') # SSH transfer transport = Paramiko. Transport ((config[' hostname '],config[' Port ')) Transport.connect (username=config[' username '],password=config[' Password ']) sftp = Paramiko. Sftpclient.from_transport (transport) # Check if the root directory exists Root_path = config[' Ssh_path '] + config[' project_name ']stdin, stdout, stderr = Ssh.exec_command (' find ' + root_path) result = Stdout.read (). Decode (' Utf-8 ') if len (result) = = 0:p rint (' directory%s does not exist, Create directory '% Root_path ' ssh.exec_command (' mkdir ' + root_path) print ('%s created successfully '% Root_path) else:print (' directory%s already exists, get all files '% root_p ATH) ssh_file_list = re.split (' \ n ', result) # Check folder Create_folder_num = 0for item in Folder_list:target_folder_path = Config [' Ssh_path '] + itemcreate_folder_num = create_folder_num + Check_folder (target_folder_path) # check File Update_file_num = 0for Item in File_list:if config[' ignore_file_type_list '].cOunt (Os.path.splitext (item) [1]) = = 0:local_file_path = config[' Local_path '] + Itemtarget_file_path = config[' Ssh_path ' ] + itemif config[' mandatory_update ']:sftp.put (local_file_path,target_file_path) print ('%s Force update succeeded '% (target_file_ path)) Update_file_num = update_file_num + 1else:update_file_num = update_file_num + check_file (Local_file_path, target_ File_path) Else:print ('%s in the ignored file type, so ignored '% item ') # Check if SSH has files that need to be deleted Delete_file_num = 0for item in ssh_file_list:temp = ITE M[item.find (config[' project_name '):]if folder_list.count (temp) = = 0 and File_list.count (temp) = = 0 and Temp! = config[' P Roject_name ' and temp! = ':p rint ('%s does not exist locally, delete '% item ') Ssh.exec_command (' RM-RF ' + item) Delete_file_num = Delete_file_n Um + 1end = Datetime.datetime.now () print (' End of this upload: Create folder%s, update file%s, delete file%s, time:%s '% (Create_folder_num, Update_file_ Num, Delete_file_num, end-begin)) if config[' Restart_tomcat ']:p rint (' off Tomcat ') Ssh.exec_command (' sh ' + config[') Tomcat_path '] + ' shutdown.sh ') print (' Start tomcat ') Ssh.exec_command (' sh ' + config[' tomcat_path '] + ' startup.sh ') # Close connection sftp.close () Ssh.close () 

Finally added a mandatory update, that is, regardless of 3,721 only in the Ignore list directly upload overrides, after all, by comparing size to update the file has the probability of a problem, such as I changed 1 to 2 by the file size is not comparable, if there is time after the words will be pondering the next pull git update records or other programs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.