在python进行自动化编写的过程中,常常需要造一些数据,比如,获取随机的合法IP,随机的字符串,当前的时间等,下面的一些方法应该可以用到,希望对你有所帮助
#!/user/bin/env python
#coding=utf-8
import random
import socket
import string
import struct
import os
import datetime
import copy
import time
from framework.logger import Logger
logger = Logger(logger="rcpUtils").getlog()
BASE_DIR = os.path.dirname(os.path.dirname(file))
DC_PATH = BASE_DIR + r"\config\districtcode.txt"
visaPrefixList = [
['4', '5', '3', '9'],
['4', '5', '5', '6'],
['4', '9', '1', '6'],
['4', '5', '3', '2'],
['4', '9', '2', '9'],
['4', '0', '2', '4', '0', '0', '7', '1'],
['4', '4', '8', '6'],
['4', '7', '1', '6'],
['4']]
mastercardPrefixList = [
['5', '1'], ['5', '2'], ['5', '3'], ['5', '4'], ['5', '5']]
amexPrefixList = [['3', '4'], ['3', '7']]
discoverPrefixList = [['6', '0', '1', '1']]
dinersPrefixList = [
['3', '0', '0'],
['3', '0', '1'],
['3', '0', '2'],
['3', '0', '3'],
['3', '6'],
['3', '8']]
enRoutePrefixList = [['2', '0', '1', '4'], ['2', '1', '4', '9']]
jcbPrefixList = [['3', '5']]
voyagerPrefixList = [['8', '6', '9', '9']]
MerchantNameList = ['凯总','澄邈','德泽','海超','海阳','海荣','海逸','海昌','瀚钰','瀚文','涵亮','昌盛','恨桃','依秋','依波','香巧','紫萱','涵易','忆之','幻巧','巧兰','惜蕊','雪晴','曼彤','宛秋','碧菡','若松','向秋','涵蕾','冰蝶','沛凝']
BankCardNumberlist = ['378288287735133','378271420218126','378238188701205','378207606238884','378275326032713','378258788874244','378210884152447','378224480016435','378288478747376','378232055211786']
MerchantUrlList_http = ['http://www.kaizong.com','http://www.baidu.com','http://www.jd.com','http://www.tengxun.com','http://www.alibaba.com']
MerchantUrlList_https = ['https://www.kaizong.com','https://www.baidu.com','https://www.jd.com','https://www.tengxun.com','https://www.alibaba.com']
ITList = ['441411100101148','4245245469','4245245463','4245245467','441411100101149','4245245461','441411100101146',
'4245245462','441411100101147','56214714892','111111111111','56214714890']
generator = random.Random()
generator.seed()
'''工具类'''
class rcpUtils():
def __init__(self):
pass
def AutoGeneratedString(self,number):
'''随机生成字符串方法,主要用于输入框不能超过多少字符串的场景,此一次性产生的最大的字符串是62个'''
return ''.join(random.sample(string.ascii_letters + string.digits, number))
def AutoGeneratedNumber(self,number):
return ''.join(random.sample(string.digits, number))
def get_random_ip(self):
RANDOM_IP_POOL=['192.168.10.222/0']
'''随机生成合法的IP'''
str_ip = RANDOM_IP_POOL[random.randint(0,len(RANDOM_IP_POOL) - 1)]
str_ip_addr = str_ip.split('/')[0]
str_ip_mask = str_ip.split('/')[1]
ip_addr = struct.unpack('>I',socket.inet_aton(str_ip_addr))[0]
mask = 0x0
for i in range(31, 31 - int(str_ip_mask), -1):
mask = mask | ( 1 << i)
ip_addr_min = ip_addr & (mask & 0xffffffff)
ip_addr_max = ip_addr | (~mask & 0xffffffff)
return socket.inet_ntoa(struct.pack('>I', random.randint(ip_addr_min, ip_addr_max)))
def getMerchantName(self):
#先对list去重
list(set(MerchantNameList))
return random.sample(MerchantNameList, 1)[0]
def getBankCardNumber(self):
#先对list去重
list(set(BankCardNumberlist))
return random.sample(BankCardNumberlist, 1)[0]
def getCurrentTime(self):
#2017-10-26 18:28:04,日期格式
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
def getTime(self):
return time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
def getTime_YMD(self):
'''获取当前的时间,年月日'''
return time.strftime('%Y%m%d',time.localtime(time.time()))
def getMerchantUrl_http(self):
list(set(MerchantUrlList_http))
return random.sample(MerchantUrlList_http, 1)[0]
def getMerchantUrl_https(self):
list(set(MerchantUrlList_https))
return random.sample(MerchantUrlList_https, 1)[0]
def getIT(self):
list(set(ITList))
return random.sample(ITList, 1)[0]
def get_id(self,system_id,order_id):
return order_id + '_' + system_id
def Cleaning_data_without_finger(self,RiskEventData):
if type(RiskEventData) == dict:
if RiskEventData:
NoVerificationList = ['finger_id','create_time','json','create_date','process_des','process_date','alarm_flag','id','processor_name','processor_id']
logger.info('不需要检查的字段为:%s'%NoVerificationList)
for i in NoVerificationList:
del RiskEventData[i]
logger.info('做数据清洗后的数据为:%s'%RiskEventData)
else:
logger.exception('数据没有入库!')
else:
logger.exception('传进来的不是字典类型')
return RiskEventData
def Cleaning_data_with_finger(self, RiskEventData):
if type(RiskEventData) == dict:
if RiskEventData:
NoVerificationList = ['create_time', 'json', 'create_date', 'process_des', 'process_date',
'alarm_flag', 'id', 'processor_name', 'processor_id']
logger.info('不需要检查的字段为:%s' % NoVerificationList)
for i in NoVerificationList:
del RiskEventData[i]
logger.info('做数据清洗后的数据为:%s' % RiskEventData)
else:
logger.exception('风险事件没有入库!')
else:
logger.exception('传进来的不是字典数据类型')
return RiskEventData