1 changed files with 81 additions and 0 deletions
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
from ftplib import FTP |
||||
import os |
||||
|
||||
|
||||
# 使用示例 |
||||
ftp_hosts = ["172.31.1.11","172.31.1.12","172.31.1.13","172.31.1.14","172.31.1.15","172.31.1.16","172.31.1.17"] # FTP 服务器地址 |
||||
ftp_user = 'pi' # FTP 登录用户名 |
||||
ftp_passwd = 'ph2008' # FTP 登录密码 |
||||
#获取当前文件上一层目录,即项目目录 make2 底下 |
||||
local_file = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
||||
def upload_file(ftp_host, ftp_user, ftp_passwd, local_file, remote_file): |
||||
ftp = FTP(ftp_host) |
||||
ftp.login(user=ftp_user, passwd=ftp_passwd) |
||||
|
||||
#判断 local_file 是文件还是目录 |
||||
if os.path.isdir(local_file): |
||||
if remote_file not in ftp.nlst(): |
||||
ftp.mkd(remote_file) |
||||
|
||||
# 切换至远程目录 |
||||
ftp.cwd(remote_file) |
||||
|
||||
# 遍历本地目录 |
||||
for file_name in os.listdir(local_file): |
||||
local_path = os.path.join(local_file, file_name) |
||||
|
||||
if os.path.isfile(local_path): |
||||
# 如果是文件,上传文件 |
||||
with open(local_path, 'rb') as file: |
||||
ftp.storbinary(f'STOR {file_name}', file) |
||||
ftp.quit() |
||||
return |
||||
|
||||
|
||||
# 打开本地文件 |
||||
with open(local_file, 'rb') as file: |
||||
# 上传文件到指定目录 |
||||
ftp.storbinary(f'STOR {remote_file}', file) |
||||
|
||||
ftp.quit() |
||||
|
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
|
||||
#获取输入的文件名,有路劲需要携带路劲 |
||||
local_file = input("请输入文件名/目录:例如 main_step1.py 或者 tools/ftp.py 或者目录 tools ->") |
||||
if local_file == "": |
||||
print("文件不能为空") |
||||
exit() |
||||
#本地文件的路径构造 |
||||
dirPath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))+"/" + local_file |
||||
#检测文件是否存在 |
||||
if os.path.exists(dirPath) == False: |
||||
print("文件/目录 不存在") |
||||
exit() |
||||
|
||||
#请选择是否上传到 apps 目录下 还是上传到 make2 下 |
||||
upload_path = input("请选择是否上传到 apps 目录下 还是上传到 make2 下 ->") |
||||
if upload_path != "apps" and upload_path != "make2": |
||||
print("不支持该目录上传") |
||||
exit() |
||||
|
||||
#请选择是否上传到全部的服务器 11 到 17 |
||||
isIp = input("请选择是否上传到全部的服务器 11 到 17,全部上传输入yes, 指定服务上传输入IP ->") |
||||
if isIp == "": |
||||
print("输入不能为空") |
||||
exit() |
||||
#远程服务的路径 |
||||
remote_file = "/" + upload_path + "/" + local_file |
||||
|
||||
#上传到全部的服务器上去 |
||||
if isIp == "yes": |
||||
for ftp_host in ftp_hosts: |
||||
upload_file(ftp_host, ftp_user, ftp_passwd, dirPath, remote_file) |
||||
else: |
||||
#上传到指定的服务器上去 |
||||
ftp_host = isIp |
||||
upload_file(ftp_host, ftp_user, ftp_passwd, dirPath, remote_file) |
||||
|
||||
print("上传成功") |
||||
Loading…
Reference in new issue