惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
Hacker News: Ask HN
Hacker News: Ask HN
Know Your Adversary
Know Your Adversary
C
Cisco Blogs
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
T
Tor Project blog
T
Tenable Blog
P
Privacy & Cybersecurity Law Blog
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
腾讯CDC
L
LangChain Blog
IT之家
IT之家
Recent Commits to openclaw:main
Recent Commits to openclaw:main
月光博客
月光博客
N
News and Events Feed by Topic
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Tailwind CSS Blog
Jina AI
Jina AI
S
Security Affairs
T
The Blog of Author Tim Ferriss
博客园 - Franky
H
Hacker News: Front Page
Martin Fowler
Martin Fowler
D
DataBreaches.Net
酷 壳 – CoolShell
酷 壳 – CoolShell
Webroot Blog
Webroot Blog
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
U
Unit 42
S
Schneier on Security
B
Blog
Schneier on Security
Schneier on Security
Latest news
Latest news
TaoSecurity Blog
TaoSecurity Blog
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
Recorded Future
Recorded Future
O
OpenAI News
雷峰网
雷峰网
H
Heimdal Security Blog

Mycpen

13_Hexo-操作记录 12_Hexo-GitHub Actions 发布博客静态资源至 NPM 06_转载-自我提升的8个好习惯 05_转载-延寿指南-反向面试-文档写作规范 04_转载-提问的智慧 03_转载-中文文案排版指北 11_Hexo-GitHub Actions 自动部署博客 10_Hexo-GitHub Actions 自动刷新多吉云 CDN 缓存 07_Other-文章记录-sshpass 03_Error-问题记录-winpty-.gitignore 02_转载-Replit 部署 Memos & Butterfly 基于 Memos 实现清单功能 09_Hexo-Replit 搭建 Uptime Kuma 监控服务 08_Hexo-博客引用 B站鸿蒙字体 07_Hexo-插件整理 06_Other-Windows 图片批量压缩工具 ultra-tinypng 01_转载-图床搭建 Backblaze B2 + Cloudflare CDN 05_Other-使用 Python open 函数批量替换文件内容 04_Other-使用 Python ftplib 模块同步又拍云云存储数据至本地 03_Other-PC端环境记录
08_Other-GitHub Actions 定时执行脚本,备份又拍云云存储
Mycpen · 2023-04-02 · via Mycpen

复制以下我的 yml 示例;或是按照 这篇文章,选择想要的 workflow 模板,再自定义修改内容(Actions => New workflow = Choose a workflow)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294




from cmath import log
from ftplib import FTP
import os
import sys
import time
import socket
import subprocess

class MyFTP:
"""
ftp自动下载、自动上传脚本,可以递归目录操作
作者:欧阳鹏
博客地址:http://blog.csdn.net/ouyang_peng/article/details/79271113
"""

def __init__(self, host, port=21):
""" 初始化 FTP 客户端
参数:
host:ip地址

port:端口号
"""


self.host = host
self.port = port
self.ftp = FTP()


self.ftp.encoding = 'utf8'

path = os.path.dirname(os.path.realpath(__file__))

self.file_list = []

def login(self, username, password):
""" 初始化 FTP 客户端
参数:
username: 用户名

password: 密码
"""
try:
timeout = 60
socket.setdefaulttimeout(timeout)

self.ftp.set_pasv(True)



self.debug_print('开始尝试连接到 %s' % self.host)
self.ftp.connect(self.host, self.port)
self.debug_print('成功连接到 %s' % self.host)

self.debug_print('开始尝试登录到 %s' % self.host)
self.ftp.login(username, password)
self.debug_print('成功登录到 %s' % self.host)

self.debug_print(self.ftp.welcome)
except Exception as err:
self.deal_error("FTP 连接或登录失败 ,错误描述为:%s" % err)
pass

def is_same_size(self, local_file, remote_file):
"""判断远程文件和本地文件大小是否一致

参数:
local_file: 本地文件

remote_file: 远程文件
"""
try:
remote_file_size = self.ftp.size(remote_file)
except Exception as err:

remote_file_size = -1

try:
local_file_size = os.path.getsize(local_file)
except Exception as err:

local_file_size = -1

self.debug_print('local_file_size:%d , remote_file_size:%d' % (local_file_size, remote_file_size))
if remote_file_size == local_file_size:
return 1
else:
return 0

def download_file(self, local_file, remote_file):
"""从ftp下载文件
参数:
local_file: 本地文件

remote_file: 远程文件
"""
self.debug_print("download_file()---> local_path = %s ,remote_path = %s" % (local_file, remote_file))

if self.is_same_size(local_file, remote_file):
self.debug_print('%s 文件大小相同,无需下载' % local_file)
return
else:
try:
self.debug_print('>>>>>>>>>>>>下载文件 %s ... ...' % local_file)
buf_size = 1024
file_handler = open(local_file, 'wb')
self.ftp.retrbinary('RETR %s' % remote_file, file_handler.write, buf_size)
file_handler.close()
except Exception as err:
self.debug_print('下载文件出错,出现异常:%s ' % err)
return

def download_file_tree(self, local_path, remote_path):
"""从远程目录下载多个文件到本地目录
参数:
local_path: 本地路径

remote_path: 远程路径
"""
print("download_file_tree()---> local_path = %s ,remote_path = %s" % (local_path, remote_path))
try:
self.ftp.cwd(remote_path)
except Exception as err:
self.debug_print('远程目录%s不存在,继续...' % remote_path + " ,具体错误描述为:%s" % err)
return

if not os.path.isdir(local_path):
self.debug_print('本地目录%s不存在,先创建本地目录' % local_path)
os.makedirs(local_path)

self.debug_print('切换至目录: %s' % self.ftp.pwd())

self.file_list = []

self.ftp.dir(self.get_file_list)

remote_names = self.file_list
self.debug_print('远程目录 列表: %s' % remote_names)
for item in remote_names:
file_type = item[0]
file_name = item[1]
local = os.path.join(local_path, file_name)
if file_type == 'd':
print("download_file_tree()---> 下载目录: %s" % file_name)
self.download_file_tree(local, file_name)
elif file_type == '-':
print("download_file()---> 下载文件: %s" % file_name)
self.download_file(local, file_name)
self.ftp.cwd("..")
self.debug_print('返回上层目录 %s' % self.ftp.pwd())
return True

def upload_file(self, local_file, remote_file):
"""从本地上传文件到ftp

参数:
local_path: 本地文件

remote_path: 远程文件
"""
if not os.path.isfile(local_file):
self.debug_print('%s 不存在' % local_file)
return

if self.is_same_size(local_file, remote_file):
self.debug_print('跳过相等的文件: %s' % local_file)
return

buf_size = 1024
file_handler = open(local_file, 'rb')
self.ftp.storbinary('STOR %s' % remote_file, file_handler, buf_size)
file_handler.close()
self.debug_print('上传: %s' % local_file + "成功!")

def upload_file_tree(self, local_path, remote_path):
"""从本地上传目录下多个文件到ftp
参数:

local_path: 本地路径

remote_path: 远程路径
"""
if not os.path.isdir(local_path):
self.debug_print('本地目录 %s 不存在' % local_path)
return

self.ftp.cwd(remote_path)
self.debug_print('切换至远程目录: %s' % self.ftp.pwd())

local_name_list = os.listdir(local_path)
for local_name in local_name_list:
src = os.path.join(local_path, local_name)
if os.path.isdir(src):
try:
self.ftp.mkd(local_name)
except Exception as err:
self.debug_print("目录已存在 %s ,具体错误描述为:%s" % (local_name, err))
self.debug_print("upload_file_tree()---> 上传目录: %s" % local_name)
self.upload_file_tree(src, local_name)
else:
self.debug_print("upload_file_tree()---> 上传文件: %s" % local_name)
self.upload_file(src, local_name)
self.ftp.cwd("..")

def close(self):
""" 退出ftp
"""
self.debug_print("close()---> FTP退出")
self.ftp.quit()


def debug_print(self, s):
""" 打印日志
"""
self.write_log(s)

def deal_error(self, e):
""" 处理错误异常
参数:
e:异常
"""
log_str = '发生错误: %s' % e
self.write_log(log_str)
sys.exit()

def write_log(self, log_str):
""" 记录日志
参数:
log_str:日志
"""
time_now = time.localtime()
date_now = time.strftime('%Y-%m-%d', time_now)
format_log_str = "%s ---> %s \n " % (date_now, log_str)
print(format_log_str)


def get_file_list(self, line):
""" 获取文件列表
参数:
line:
"""
file_arr = self.get_file_name(line)

if file_arr[1] not in ['.', '..']:
self.file_list.append(file_arr)

def get_file_name(self, line):
""" 获取文件名
参数:
line:
"""
pos = line.rfind(':')
while (line[pos] != ' '):
pos += 1
while (line[pos] == ' '):
pos += 1
file_arr = [line[0], line[pos:]]
return file_arr


if __name__ == "__main__":

path = os.path.dirname(os.path.realpath(__file__))






upyunUsername = os.environ["UPYUNUSERNAME"]
upyunPassword = os.environ["UPYUNPASSWORD"]

my_ftp = MyFTP("v0.ftp.upyun.com")
my_ftp.login(upyunUsername, upyunPassword)



if os.sep == "\\":
pass
elif os.sep == "/":
my_ftp.download_file_tree("image/", "/image/")



if os.sep == "\\":
pass
elif os.sep == "/":
my_ftp.upload_file_tree("image/", "/image/")
my_ftp.close()