背景
测试过程中需要一些随机数据,使用到了python中的string模块,记录一下
#! /usr/bin/python
# coding:utf-8
"""
@author:Bingo.he
@file: data_factory.py
@time: 2017/12/21
"""
import socket
import struct
import string
import random
def random_phoneNo():
"""获取随机号码"""
return '1' + ''.join(random.choice(string.digits) for i in range(10))
def random_upper_code(length):
"""获取大写字母与数字的随机码"""
return ''.join(random.choice(string.ascii_uppercase + string.digits) for i in range(length))
def random_lower_code(length):
"""获取小写字母与数字的随机码"""
return ''.join(random.choice(string.ascii_lowercase + string.digits) for i in range(length))
def random_IP():
"""获取随机IP"""
RANDOM_IP_POOL = ['202.69.19.222/0']
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)))
PS: 博主公司使用的手机号校验规则为了适应新号段层出不穷的情况,此前升级为1开头、11位数字组成即可通过校验