1、生成随机字符串
1 #数字+字母+符号 2 def getRandChar(n): 3 l = [] 4 #sample = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()-+=.' 5 sample = random.sample(string.ascii_letters + string.digits, 62)## 从a-zA-Z0-9生成指定数量的随机字符: list类型 6 sample = sample + list('!@#$%^&*()-+=.')#原基础上加入一些符号元素 7 for i in range(n): 8 char = random.choice(sample)#从sample中选择一个字符 9 l.append(char) 10 return ''.join(l)#返回字符串
2、生成指定数量的随机字符
1 # 从a-zA-Z0-9生成指定数量的随机字符: 2 ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 8))
3、生成随机字符
1 # 随机字符: 2 alphabet = 'abcdefghijklmnopqrstuvwxyz!@#$%^&*()' 3 char = random.choice(alphabet)
4、生成一个ipv4
1 def generateIpv4(): 2 a = random.randint(0,255) 3 b = random.randint(0,255) 4 c = random.randint(0,255) 5 d = random.randint(0,255) 6 7 ipv4 = '%d.%d.%d.%d'%(a,b,c,d) 8 return ipv4