标签:upload makedirs targe city androi mobile names soc 获取
检测邮箱是否合法
1 def check_email(email): 2 import re 3 res = re.match(r‘^[0-9a-zA-Z\_\-]+(\.[0-9a-zA-Z\_\-]+)*@[0-9a-zA-Z]+(\.[0-9a-zA-Z]+){1,}$‘,email) 4 return True if res else False
检测手机号码是否合法
1 def check_phone(phone): 2 import re 3 res = re.match(r‘^1[34578]\d{9}$‘,phone) 4 return True if res else False
判断手机是安卓系统还是苹果系统或者是PC端
1 def check_mobile_model(raw): 2 import re 3 try: 4 model = re.search(‘Android‘, raw) 5 if not model: 6 model = re.search(‘iPhone‘, raw) 7 return model.group() 8 except: 9 return ‘PC‘
根据IP获取地理位置
1 def find_ip(ip): 2 try: 3 import geoip2.database 4 reader = geoip2.database.Reader(‘app/data/GeoLite2-City.mmdb‘) 5 info = reader.city(ip) 6 reader.close() 7 return info.country.names[‘zh-CN‘] 8 except Exception as e: 9 print(e) 10 return ‘未知‘
检测url是否合法
1 def check_url(url): 2 import re 3 res = re.match(r‘^[a-zA-z]+://[^\s]*$‘,url) 4 return True if res else False
获取当前格式化时间
1 def get_current_time(): 2 from datetime import datetime 3 return datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)
获取N天前时间
1 def getBeforeNdays(N=0): 2 from datetime import datetime, timedelta 3 return (datetime.now()-timedelta(days=N)).strftime(‘%Y-%m-%d %H:%M:%S‘)
获取客户端IP
1 def get_remote_ip(): 2 import socket 3 ip = socket.gethostbyname(socket.gethostname()) 4 return ip
获取客户端地理位置
安装扩展pip install geoip2
1 def find_position_by_ip(ip): 2 try: 3 import geoip2.database 4 reader = geoip2.database.Reader(‘path/GeoLite2-City.mmdb‘) 5 info = reader.city(ip) 6 data = {‘continent‘: info.continent.names[‘zh-CN‘], 7 ‘country‘: info.country.names[‘zh-CN‘], 8 ‘city‘: info.subdivisions[0].names[‘zh-CN‘], 9 ‘latitude‘: info.location.latitude, 10 ‘longitude‘: info.location.longitude 11 } 12 reader.close() 13 except: 14 data = {} 15 return data
获取客户端UA信息
# 从http 协议 headers 中 获取 User-Agent 配置
判断目录是否存在,若不存在则创建
1 def is_dir_exists(path): 2 import os 3 if not os.path.exists(path): 4 os.makedirs(path)
文件生成文件名并上传
1 def upload(file): 2 from datetime import datetime 3 import random 4 # 指定存储路径,如不存在需要创建 5 path = ‘path/‘ 6 # 获取文件的后缀名 7 try: 8 ext = ‘.‘ + file.filename.rsplit(‘,‘,1)[1] 9 except: 10 ext = ‘.png‘ 11 # 组装新的文件名,生成格式 日时分秒 + 五位随机数 + 后缀 12 filename = datetime.now().strftime(‘%d%H%M%S‘)+‘-‘+str(random.randint(10000,99999)) 13 file_upload_path = path + filename 14 with open(file_upload_path,‘wb‘) as fp: 15 fp.write(file.read()) 16 # 返回文件名 17 return filename
无限极分类
#数据源 ‘‘‘ [ {‘id‘: 22, ‘name‘: ‘北京‘, ‘pid‘: 0}, {‘id‘: 23, ‘name‘: ‘辽宁省‘, ‘pid‘: 0}, {‘id‘: 24, ‘name‘: ‘河北省‘, ‘pid‘: 0}, {‘id‘: 26, ‘name‘: ‘沈阳‘, ‘pid‘: 23}, {‘id‘: 27, ‘name‘: ‘石家庄‘, ‘pid‘: 24}, {‘id‘: 29, ‘name‘: ‘朝阳‘, ‘pid‘: 23}, {‘id‘: 30, ‘name‘: ‘大连‘, ‘pid‘: 23}, {‘id‘: 31, ‘name‘: ‘朝阳区‘, ‘pid‘: 22}, {‘id‘: 32, ‘name‘: ‘海淀区‘, ‘pid‘: 22}, {‘id‘: 33, ‘name‘: ‘五道口‘, ‘pid‘: 32}, {‘id‘: 34, ‘name‘: ‘雙塔‘, ‘pid‘: 29}, {‘id‘: 36, ‘name‘: ‘金州‘, ‘pid‘: 30}, {‘id‘: 38, ‘name‘: ‘中国‘, ‘pid‘: 0}] ‘‘‘
1 #实现代码 2 def get_infinite_level(data,pid=0,deep=0): 3 tree = [] 4 for row in data: 5 if row[‘pid‘] == pid: 6 row[‘deep‘] = deep 7 row[‘child‘] = get_infinite_level(data,row[‘id‘],deep+1) 8 tree.append(row) 9 return tree
#实现结果 [{ ‘id‘: 22, ‘name‘: ‘北京‘, ‘pid‘: 0, ‘deep‘: 0, ‘child‘: [{ ‘id‘: 31, ‘name‘: ‘朝阳区‘, ‘pid‘: 22, ‘deep‘: 1, ‘child‘: [] }, { ‘id‘: 32, ‘name‘: ‘海淀区‘, ‘pid‘: 22, ‘deep‘: 1, ‘child‘: [{ ‘id‘: 33, ‘name‘: ‘五道口‘, ‘pid‘: 32, ‘deep‘: 2, ‘child‘: [] }] }] }, { ‘id‘: 23, ‘name‘: ‘辽宁省‘, ‘pid‘: 0, ‘deep‘: 0, ‘child‘: [{ ‘id‘: 26, ‘name‘: ‘沈阳‘, ‘pid‘: 23, ‘deep‘: 1, ‘child‘: [] }, { ‘id‘: 29, ‘name‘: ‘朝阳‘, ‘pid‘: 23, ‘deep‘: 1, ‘child‘: [{ ‘id‘: 34, ‘name‘: ‘雙塔‘, ‘pid‘: 29, ‘deep‘: 2, ‘child‘: [] }] }, { ‘id‘: 30, ‘name‘: ‘大连‘, ‘pid‘: 23, ‘deep‘: 1, ‘child‘: [{ ‘id‘: 36, ‘name‘: ‘金州‘, ‘pid‘: 30, ‘deep‘: 2, ‘child‘: [] }] }] }, { ‘id‘: 24, ‘name‘: ‘河北省‘, ‘pid‘: 0, ‘deep‘: 0, ‘child‘: [{ ‘id‘: 27, ‘name‘: ‘石家庄‘, ‘pid‘: 24, ‘deep‘: 1, ‘child‘: [] }] }, { ‘id‘: 38, ‘name‘: ‘中国‘, ‘pid‘: 0, ‘deep‘: 0, ‘child‘: [] }] ‘‘‘
获取树状结构
#数据源 ‘‘‘ [ {‘id‘: 22, ‘name‘: ‘北京‘, ‘pid‘: 0}, {‘id‘: 23, ‘name‘: ‘辽宁省‘, ‘pid‘: 0}, {‘id‘: 24, ‘name‘: ‘河北省‘, ‘pid‘: 0}, {‘id‘: 26, ‘name‘: ‘沈阳‘, ‘pid‘: 23}, {‘id‘: 27, ‘name‘: ‘石家庄‘, ‘pid‘: 24}, {‘id‘: 29, ‘name‘: ‘朝阳‘, ‘pid‘: 23}, {‘id‘: 30, ‘name‘: ‘大连‘, ‘pid‘: 23}, {‘id‘: 31, ‘name‘: ‘朝阳区‘, ‘pid‘: 22}, {‘id‘: 32, ‘name‘: ‘海淀区‘, ‘pid‘: 22}, {‘id‘: 33, ‘name‘: ‘五道口‘, ‘pid‘: 32}, {‘id‘: 34, ‘name‘: ‘雙塔‘, ‘pid‘: 29}, {‘id‘: 36, ‘name‘: ‘金州‘, ‘pid‘: 30}, {‘id‘: 38, ‘name‘: ‘中国‘, ‘pid‘: 0} ] ‘‘‘
1 #实现代码 2 def get_tree_struct(data,pid=0,level=0): 3 data_list = [] 4 for info in data: 5 if info[‘pid‘] == pid: 6 info[‘level‘] = level 7 info[‘name‘] = ‘^-^ ‘*level + info[‘name‘] 8 data_list.append(info) 9 data_list += get_tree_struct(data, info[‘id‘], level + 1) 10 return data_list
‘‘‘ [{ ‘id‘: 22, ‘name‘: ‘北京‘, ‘pid‘: 0, ‘level‘: 0 }, { ‘id‘: 31, ‘name‘: ‘^-^ 朝阳区‘, ‘pid‘: 22, ‘level‘: 1 }, { ‘id‘: 32, ‘name‘: ‘^-^ 海淀区‘, ‘pid‘: 22, ‘level‘: 1 }, { ‘id‘: 33, ‘name‘: ‘^-^ ^-^ 五道口‘, ‘pid‘: 32, ‘level‘: 2 }, { ‘id‘: 23, ‘name‘: ‘辽宁省‘, ‘pid‘: 0, ‘level‘: 0 }, { ‘id‘: 26, ‘name‘: ‘^-^ 沈阳‘, ‘pid‘: 23, ‘level‘: 1 }, { ‘id‘: 29, ‘name‘: ‘^-^ 朝阳‘, ‘pid‘: 23, ‘level‘: 1 }, { ‘id‘: 34, ‘name‘: ‘^-^ ^-^ 雙塔‘, ‘pid‘: 29, ‘level‘: 2 }, { ‘id‘: 30, ‘name‘: ‘^-^ 大连‘, ‘pid‘: 23, ‘level‘: 1 }, { ‘id‘: 36, ‘name‘: ‘^-^ ^-^ 金州‘, ‘pid‘: 30, ‘level‘: 2 }, { ‘id‘: 24, ‘name‘: ‘河北省‘, ‘pid‘: 0, ‘level‘: 0 }, { ‘id‘: 27, ‘name‘: ‘^-^ 石家庄‘, ‘pid‘: 24, ‘level‘: 1 }, { ‘id‘: 38, ‘name‘: ‘中国‘, ‘pid‘: 0, ‘level‘: 0 }] ‘‘‘
标签:upload makedirs targe city androi mobile names soc 获取
原文地址:https://www.cnblogs.com/a2534786642/p/11040566.html