url = 'http://localhost.com'
# 拼接網址函數
def makeurl(*res):
lst_res = list(res)
for i in range(0,len(lst_res)):
res = lst_res[i]
if res.endswith('/') or res.startswith('/'):
lst_res[i] == res.strip('/')
lst_res.insert(0,url)
return '/'.join(lst_res)
url = makeurl('ceshi','logins')
請求重試裝飾器
# 連接重試裝飾器
def retry(exception=Timeout,max_times=3):
def inner(func):
def warp(*args,**kwargs):
for try_time in range(max_times):
try:
return func(*args,**kwargs)
except exception as e:
if try_time == max_times -1:
raise e
time.sleep(3)
print('重試上一次請求')
continue
return warp
return inner
復制文件
# 復制文件
def copy_file(source_file, des_file):
"""復制文件"""
from shutil import copyfile
import sys
try:
copyfile(source_file, des_file)
except IOError as e:
print("Unable to copy file. %s" % e)
raise e
except Exception as e:
print("Unexpected error:", sys.exc_info())
raise e
copy_file(source_file, des_file)
創建文件夾
# 創建文件夾
def makedir(path):
# 去除首尾空格
path = path.strip()
# 去除尾部 \ 符號
path.rstrip('\\')
if not os.path.exists(path):
os.makedirs(path)
return path