拼接网址函数
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
创建日志
import logging
import os.path
from logging.handlers import TimedRotatingFileHandler
path = r"C:\Users\24119\Desktop\test"
def mkdir(path):
path = path.replace(" ", "")
if not os.path.exists(path):
os.makedirs(path)
return path
def config_log(basedir=None):
if not basedir:
basedir = os.path.abspath('')
filename = os.path.join(basedir, 'info.log')
log_file_handler = TimedRotatingFileHandler(filename=filename, encoding='utf-8', when="D", interval=2,
backupCount=30)
fmt = logging.Formatter(fmt='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
log_file_handler.setFormatter(fmt)
logging.basicConfig(level=logging.INFO, handlers=[log_file_handler])
log_dir = os.path.abspath("")
config_log(log_dir)
logging.warning("wqdwad")
logging.info("123")
url编码
import urllib.parse
encode = urllib.parse.quote("发动机")
print(encode)
decode = urllib.parse.unquote("%E5%8F%91%E5%8A%A8%E6%9C%BA")
print(decode)