利用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::%s@%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::ahfvd@qgoefgyxbf.com::570505799633973-5-10
Fri Aug 28 21:00:12 149488065::ypliyfg@brmgaaig.net::4717325545362012-7-8
Tue Aug 29 19:13:28 189633561::xwoxq@ixrfi.net::5984195035662808-5-5
Sun Mar 9 16:01:45 127723924::svev@ytiqxqemalrp.org::4030515577555305-4-12
Sun Feb 24 22:02:17 15157754::txkn@ykwbikcbeapj.com::478270352959337-4-12
Sat Apr 1 02:16:35 71501341::bksggqn@jsykydqtzkjd.gov::2256302226478595-7-12
Sun Jul 6 05:02:28 18939664::yoxlbzt@nsxwkkispsyb.com::597615916712548-7-12
Wed Jun 29 17:17:14 114444146::saqfaj@zahfeokrohmn.gov::3611446270334234-6-12
Fri May 19 15:08:14 234054828::tydg@tvbgjmaxhpjh.gov::7385994817398494-4-12
Fri Nov 16 04:22:12 269978536::kxidgub@qrxydwffkn.edu::8519637561999732-7-10
Fri Jul 8 08:51:02 132470935::fdqpt@ktpgxag.org::4180316786239862-5-7
Wed Aug 10 16:48:49 36911560::egulsg@goodvga.edu::1164754179132529-6-7
Sat Nov 29 23:20:26 149477394::xghxqb@ciornmdydk.org::4716988809146426-6-10
Fri Jul 7 13:19:11 152052986::iesf@rgfusbbbvx.edu::4798266629692751-4-10
Fri Nov 20 18:10:08 184687953::znpxukm@bmrwtlog.edu::5828126728558208-7-8
Thu May 14 20:58:39 74067665::fydupp@htpygpdjg.org::2337287593582719-6-9
Wed Dec 4 16:20:16 6375405::stdrqp@ematyjde.gov::201126211518016-6-8
Tue Oct 2 21:39:46 105796046::yfwpgp@jaajtaeb.com::3338538601988386-6-8
Wed Oct 29 01:21:40 242249941::elio@pxrkwppeutnq.edu::7644607618929700-4-12
Fri Oct 28 14:23:42 225762907::kube@xbrldbnh.edu::7124327078307822-4-8
Fri May 17 05:26:04 237385793::dryteu@thtqbwjb.gov::7491109919808364-6-8
Sat Nov 9 06:58:43 32473963::zgiblz@srudpmx.edu::1024717151372323-6-7
Tue Jan 29 11:24:31 48403935::tpwan@ixqgar.edu::1527418488597871-5-6
Sat Jul 16 01:41:29 63586450::pxusn@oiatmeawusm.net::2006532400268489-5-11
Tue Jun 17 15:53:16 125570492::ygspkq@dqruehtdve.net::3962559836044396-6-10
Fri Jul 6 18:02:18 110124485::lltpzu@axyyicbojkt.org::3475130936176938-6-11
Mon Nov 6 02:59:19 128389967::jrevq@brgkadkgufd.org::4051533885361159-5-11
Wed Aug 29 07:26:21 231020587::wzxk@nmmbzxx.edu::7290243428541981-4-7
Wed Feb 12 04:46:47 44084358::zgek@puujor.gov::1391105805770807-4-6
Thu Mar 6 21:35:05 269774380::snagtj@dezvtkphuzo.edu::8513194998922505-6-11
Sun Aug 1 04:50:36 5675115::lcidrip@djiieqhgrn.com::179027182702236-7-10
Fri Jan 30 11:55:08 173445807::rlyux@xhwgfddvlco.edu::5473358841383708-5-11
Mon Nov 23 15:06:56 92711344::fvcud@krqmogw.net::2925625293529616-5-7
Fri Sep 1 18:57:03 122804541::zqehfs@rcyddelnh.gov::3875274859604223-6-9
Thu Aug 19 06:30:59 225684934::jhozy@ijjiupyjvcw.gov::7121866482052259-5-11
Tue Jan 13 23:40:47 220538415::tbqhg@vadjxsvxuw.com::6959458010216447-5-10
Mon Jan 29 19:35:42 156070638::lboijsg@vahdhbm.org::4925051467241742-7-7
Tue Jun 9 14:24:17 10843671::fwanqzg@gytegglv.org::342131051859857-7-8
Mon Oct 21 00:17:29 28336307::edilf@ewfwidreylj.net::894145337885849-5-11
Wed Aug 7 11:14:04 290314458::yfvoxn@gavgkqnuljb.gov::9161377267720444-6-11
Sun Nov 4 20:56:12 249269523::lratvzz@yqsfhrqqbe.com::7866124231697772-7-10
Sun Oct 31 16:30:24 151044100::lyexl@bmege.net::4766429272638624-5-5
Tue Mar 22 17:36:01 205694935::jvijdr@firuvgoonzzd.org::6491043030188161-6-12
Sat Mar 1 10:14:01 99822200::pucbbnq@nhcyylptxyh.org::3150022211864041-7-11
Mon Jan 8 03:56:40 58937049::vxhrbea@wjigyufx.net::1859811459739000-7-8
Tue Jul 3 23:50:07 141736610::ekhy@oynkrh.com::4472713247068207-4-6
Wed Apr 5 02:00:40 211115786::jfwetyt@wrgmmvn.net::6662108566202440-7-7
Sat Mar 6 15:37:16 227311288::qhne@ojnknexf.net::7173189242955436-4-8
Wed Mar 10 00:24:24 220025756::pwlip@ryzwucuxhcwg.org::6943280059585464-5-12
Wed Jun 21 17:55:56 164719426::zlew@wjtjqvm.org::5197980867357356-4-7
Fri Sep 14 08:34:01 219967392::klaqp@hzgegttbqapj.gov::6941438285934841-5-12
Fri Feb 15 10:49:01 200550532::ssqag@skqddvk.edu::6328701348576541-5-7
Tue Nov 4 23:24:52 141649519::crjxp@rosolunkc.edu::4469964931236292-5-9
Thu May 26 12:30:05 82025836::lqddxnm@ohmzauofisgj.edu::2588423214803405-7-12
Wed Dec 23 18:00:32 127106522::ynkp@usruda.gov::4011032277252032-4-6
Mon Feb 1 03:20:18 25237909::uqiulc@vscgxwjpf.edu::796369318341618-6-9
Tue Nov 21 08:08:53 93402215::xnrey@tovrxfxks.net::2947427076211733-5-9
Thu Jul 27 19:23:06 134687724::txjdc@djaxu.edu::4250271892044186-5-5
Mon Mar 14 23:19:16 228798687::mjvd@dieplzicmcl.org::7220127022413556-4-11
Wed Mar 31 03:42:05 209315677::wvwg@cwxcebihymce.edu::6605302612506125-4-12
Wed Nov 8 00:01:06 236681578::puyepci@ycfgepc.org::7468887055881666-7-7 Process finished with exit code 0

上述参考自:Python核心编程

谢谢

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

  1. python 生成随机字符串

    1.生成随机字符串 #数字+字母+符号 def getRandChar(n): l = [] #sample = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^ ...

  2. python生成随机日期字符串

    python生成随机日期字符串 生成随机的日期字符串,用于插入数据库. 通过时间元组设定一个时间段,开始和结尾时间转换成时间戳. 时间戳中随机取一个,再生成时间元组,再把时间元组格式化输出为字符串 # ...

  3. .net生成随机字符串

    生成随机字符串的工具类: /// <summary> /// 随机字符串工具类 /// </summary> public class RandomTools { /// &l ...

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

    说明:生成随机字符串用到的方法有 mt_rand() 生成唯一字符串用到的方法有 md5(),uniqid(),microtime() 代码: <?php /* * 生成随机字符串 * @par ...

  5. PHP生成随机字符串包括大小写字母

    PHP生成随机字符串包括大小写字母,这里介绍两种方法: 第一种:利用字符串函数操作 <?php /** *@blog <www.phpddt.com> */ function cre ...

  6. 生成随机字符串(UUID方法)

    这是另一种用UUID生成随机字符串的方法. public class RandomGenerator{ private int length; public void setLength(int le ...

  7. SQL生成随机字符串

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

  8. Python 生成随机验证码

    Python生成随机验证码  Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...

  9. Python生成随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...

随机推荐

  1. 错误代码:0x80070032 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

    错误分析: vs2010默认采用的是.NET 4.0框架,4.0框架是独立的CLR,和.NET 2.0的不同,如果想运行.NET 4.0框架的网站,需要用aspnet_regiis注册.NET 4.0 ...

  2. Windows 7 下如何配置 java 环境变量

    安装 JDK.从Oracel官方网站上下载,下载完成后安装. http://www.oracle.com/technetwork/java/javase/downloads/jdk8-download ...

  3. 南阳219----An problem about date

    /* 1600年一月一日为星期6,所以算出ymd与1600-1-1差多少天对7取余即可 */ #include<stdio.h> ]={}; int main() { ,ans; ; i& ...

  4. BZOJ.3307.雨天的尾巴(dsu on tree/线段树合并)

    BZOJ 洛谷 \(dsu\ on\ tree\).(线段树合并的做法也挺显然不写了) 如果没写过\(dsu\)可以看这里. 对修改操作做一下差分放到对应点上,就成了求每个点子树内出现次数最多的颜色, ...

  5. 1489 ACM 贪心

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1489 题意:为负数表示买酒,正数表示买酒,每两家人之间为one unit of work.问最小的work ...

  6. input输入框只能输入数字和 小数点后两位

    //input输入框只能输入数字和 小数点后两位 function num(obj,val){ obj.value = obj.value.replace(/[^\d.]/g,"" ...

  7. Codeforces Round #428 (Div. 2) D. Winter is here 容斥

    D. Winter is here 题目连接: http://codeforces.com/contest/839/problem/D Description Winter is here at th ...

  8. 全排列 ---java

    排列的一种好方法,用链表来记录数据,简单明了,简称模板,值得记录 public class main{ static int count=0; public static void f(List< ...

  9. 【状压dp】Trie 树 @中山纪念中学20170304

    目录 Trie 树 PROBLEM 题目描述 输入 输出 样例输入 样例输出 SOLUTION CODE Trie 树 PROBLEM 题目描述 字母(Trie)树是一个表示一个字符串集合中所有字符串 ...

  10. pygame 笔记-6 碰撞检测

    这一节学习碰撞检测,先看原理图: 2个矩形如果发生碰撞(即:图形有重叠区域),按上图的判断条件就能检测出来,如果是圆形,则稍微变通一下,用半径检测.如果是其它不规则图形,大多数游戏中,并不要求精确检测 ...