Python生成随机字符串

利用Python生成随机域名等随机字符串。

#!/usr/bin/env python# -*- coding: utf-8 -*-
from random import randrange, choice
from string import ascii_lowercase as lc
from sys import maxsize
from time import ctime

tlds = (‘com‘, ‘edu‘, ‘net‘, ‘org‘, ‘gov‘)

for i in range(randrange(5, 110)):
    dtint = randrange(maxsize/1000)  # pick date
    dtstr = ctime(dtint)        # date string
    llen = randrange(4, 8)      # login is shorter
    login = ‘‘.join(choice(lc) for j in range(llen))
    dlen = randrange(llen, 13)
    dom = ‘‘.join(choice(lc) for j in range(dlen))
    print(‘%s::%[email protected]%s.%s::%d-%d-%d‘ % (dtstr, login, dom, choice(tlds), dtint, llen, dlen))

该脚本生成拥有三个字段的字符串,由一对冒号或者一对双冒号分隔。第一个字段是随机整数,该整数将被转换为一个日期。下一个字段是一个随机生成的电子邮件地址。最后一个字段是一个由单横线(-)分隔的整数集。

输出结果如下

/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/baoshan/Documents/03.python/Python入门经典/python/test.py
Sat Feb  6 23:39:33 18080579::[email protected]::570505799633973-5-10
Fri Aug 28 21:00:12 149488065::[email protected]::4717325545362012-7-8
Tue Aug 29 19:13:28 189633561::[email protected]::5984195035662808-5-5
Sun Mar  9 16:01:45 127723924::[email protected]::4030515577555305-4-12
Sun Feb 24 22:02:17 15157754::[email protected]::478270352959337-4-12
Sat Apr  1 02:16:35 71501341::[email protected]::2256302226478595-7-12
Sun Jul  6 05:02:28 18939664::[email protected]::597615916712548-7-12
Wed Jun 29 17:17:14 114444146::[email protected]::3611446270334234-6-12
Fri May 19 15:08:14 234054828::[email protected]::7385994817398494-4-12
Fri Nov 16 04:22:12 269978536::[email protected]::8519637561999732-7-10
Fri Jul  8 08:51:02 132470935::[email protected]::4180316786239862-5-7
Wed Aug 10 16:48:49 36911560::[email protected]::1164754179132529-6-7
Sat Nov 29 23:20:26 149477394::[email protected]::4716988809146426-6-10
Fri Jul  7 13:19:11 152052986::[email protected]::4798266629692751-4-10
Fri Nov 20 18:10:08 184687953::[email protected]::5828126728558208-7-8
Thu May 14 20:58:39 74067665::[email protected]::2337287593582719-6-9
Wed Dec  4 16:20:16 6375405::[email protected]::201126211518016-6-8
Tue Oct  2 21:39:46 105796046::[email protected]::3338538601988386-6-8
Wed Oct 29 01:21:40 242249941::[email protected]::7644607618929700-4-12
Fri Oct 28 14:23:42 225762907::[email protected]::7124327078307822-4-8
Fri May 17 05:26:04 237385793::[email protected]::7491109919808364-6-8
Sat Nov  9 06:58:43 32473963::[email protected]::1024717151372323-6-7
Tue Jan 29 11:24:31 48403935::[email protected]::1527418488597871-5-6
Sat Jul 16 01:41:29 63586450::[email protected]::2006532400268489-5-11
Tue Jun 17 15:53:16 125570492::[email protected]::3962559836044396-6-10
Fri Jul  6 18:02:18 110124485::[email protected]::3475130936176938-6-11
Mon Nov  6 02:59:19 128389967::[email protected]::4051533885361159-5-11
Wed Aug 29 07:26:21 231020587::[email protected]::7290243428541981-4-7
Wed Feb 12 04:46:47 44084358::[email protected]::1391105805770807-4-6
Thu Mar  6 21:35:05 269774380::[email protected]::8513194998922505-6-11
Sun Aug  1 04:50:36 5675115::[email protected]::179027182702236-7-10
Fri Jan 30 11:55:08 173445807::[email protected]::5473358841383708-5-11
Mon Nov 23 15:06:56 92711344::[email protected]::2925625293529616-5-7
Fri Sep  1 18:57:03 122804541::[email protected]::3875274859604223-6-9
Thu Aug 19 06:30:59 225684934::[email protected]::7121866482052259-5-11
Tue Jan 13 23:40:47 220538415::[email protected]::6959458010216447-5-10
Mon Jan 29 19:35:42 156070638::[email protected]::4925051467241742-7-7
Tue Jun  9 14:24:17 10843671::[email protected]::342131051859857-7-8
Mon Oct 21 00:17:29 28336307::[email protected]::894145337885849-5-11
Wed Aug  7 11:14:04 290314458::[email protected]::9161377267720444-6-11
Sun Nov  4 20:56:12 249269523::[email protected]::7866124231697772-7-10
Sun Oct 31 16:30:24 151044100::[email protected]::4766429272638624-5-5
Tue Mar 22 17:36:01 205694935::[email protected]::6491043030188161-6-12
Sat Mar  1 10:14:01 99822200::[email protected]::3150022211864041-7-11
Mon Jan  8 03:56:40 58937049::[email protected]::1859811459739000-7-8
Tue Jul  3 23:50:07 141736610::[email protected]::4472713247068207-4-6
Wed Apr  5 02:00:40 211115786::[email protected]::6662108566202440-7-7
Sat Mar  6 15:37:16 227311288::[email protected]::7173189242955436-4-8
Wed Mar 10 00:24:24 220025756::[email protected]::6943280059585464-5-12
Wed Jun 21 17:55:56 164719426::[email protected]::5197980867357356-4-7
Fri Sep 14 08:34:01 219967392::[email protected]::6941438285934841-5-12
Fri Feb 15 10:49:01 200550532::[email protected]::6328701348576541-5-7
Tue Nov  4 23:24:52 141649519::[email protected]::4469964931236292-5-9
Thu May 26 12:30:05 82025836::[email protected]::2588423214803405-7-12
Wed Dec 23 18:00:32 127106522::[email protected]::4011032277252032-4-6
Mon Feb  1 03:20:18 25237909::[email protected]::796369318341618-6-9
Tue Nov 21 08:08:53 93402215::[email protected]::2947427076211733-5-9
Thu Jul 27 19:23:06 134687724::[email protected]::4250271892044186-5-5
Mon Mar 14 23:19:16 228798687::[email protected]::7220127022413556-4-11
Wed Mar 31 03:42:05 209315677::[email protected]::6605302612506125-4-12
Wed Nov  8 00:01:06 236681578::[email protected]::7468887055881666-7-7

Process finished with exit code 0

上述参考自:Python核心编程

谢谢

原文地址:https://www.cnblogs.com/zhzhang/p/10125706.html

时间: 2024-10-07 13:43:35

Python生成随机字符串的相关文章

python 生成随机字符串 和随机数字

生成随机字符串: ''.join(random.sample(string.ascii_letters + string.digits, 8)) //字符串中不会重复 for _ in range(8): s += random.choice(string.ascii_letters + string.digits) //字符串可以重复 生成随机浮点数 random.uniform(10, 20) 生成随机整数 random.randint(12, 20) 原文地址:https://www.cn

python生成随机日期字符串

原文来自:  python生成随机日期字符串 生成随机的日期字符串,用于插入数据库. 通过时间元组设定一个时间段,开始和结尾时间转换成时间戳. 时间戳中随机取一个,再生成时间元组,再把时间元组格式化输出为字符串 import time import random a1=(1976,1,1,0,0,0,0,0,0) #设置开始日期时间元组(1976-01-01 00:00:00) a2=(1990,12,31,23,59,59,0,0,0) #设置结束日期时间元组(1990-12-31 23:59

Python生成随机五位数——模仿手机验证码

使用Python生成随机的五位手机验证码. # -*- coding:utf-8 -*- #生成五位随机数,模仿手机验证码 #导入random库,可以生成随机数 import random def ran(): L = [] M = [] #通过遍历5次,生成五个元素,并插入列表L for i in range(5): L.append(random.randint(0,9)) if len(L) >= 5: break #通过遍历将L的五个元素由数字转为字符串,导入空列表M,并使用join方法

.net生成随机字符串

生成随机字符串的工具类: /// <summary> /// 随机字符串工具类 /// </summary> public class RandomTools { /// <summary> /// 随机系数 /// </summary> public static int _RandIndex = 0; #region 获取某个区间的一个随机数 /// <summary> /// 获取某个区间的一个随机数 /// </summary>

[PHP]利用openssl_random_pseudo_bytes和base64_encode函数来生成随机字符串

openssl_random_pseudo_bytes函数本身是用来生成指定个数的随机字节,因此在使用它来生成随机字符串时,还需要配合使用函数base64_encode.如下所示: public static function getRandomString($length = 42) { /* * Use OpenSSL (if available) */ if (function_exists('openssl_random_pseudo_bytes')) { $bytes = openss

PHP 生成随机字符串与唯一字符串

说明:生成随机字符串用到的方法有 mt_rand() 生成唯一字符串用到的方法有 md5(),uniqid(),microtime() 代码: 1 <?php 2 /* 3 * 生成随机字符串 4 * @param int $length 生成随机字符串的长度 5 * @param string $char 组成随机字符串的字符串 6 * @return string $string 生成的随机字符串 7 */ 8 function str_rand($length = 32, $char =

Python 生成随机验证码

Python生成随机验证码 Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 from PIL import Image img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255)) # 在图片查看器中打开 # img.show()  # 保存在本地 with open('code.png','wb') as f

python生成随机验证码

Python 生成随机验证码,需安装 PIL模块 安装: pip3 install pillow 基本使用 1,.创建图片 from PIL import Image img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255)) # 在图片查看器中打开 # img.show() # 保存在本地 with open('code.png','wb') as f: img.save(f,format='png') 2.创建画笔,用

SQL生成随机字符串

1.SQLserve生成随机字符串 SELECT replace(newid(), '-', '')