Last active
April 6, 2017 14:56
-
-
Save sanpingz/5449113 to your computer and use it in GitHub Desktop.
检测未被注册的域名
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # coding: utf-8 | |
| """ | |
| domain.py | |
| 简单的whois客户端,可用于判断域名是否被注册 | |
| python domain.py [num] # num为字母组合的个数(1,8) | |
| """ | |
| import urllib, re, os, sys | |
| url = r'http://pandavip.www.net.cn/check/check_ac1.cgi?domain=' | |
| AV = 'domain.txt' | |
| UN = 'uncheck.txt' | |
| chars1 = map(chr,range(ord('a'),ord('z')+1)) | |
| chars2 = [a+b for a in chars1 for b in chars1] | |
| chars3 = (a+b for a in chars1 for b in chars2) | |
| chars4 = (a+b for a in chars2 for b in chars2) | |
| chars5 = (a+b for a in chars2 for b in chars3) | |
| chars6 = (a+b for a in chars3 for b in chars3) | |
| chars7 = (a+b for a in chars3 for b in chars4) | |
| chars8 = (a+b for a in chars4 for b in chars4) | |
| chars = [chars1, chars2, chars3, chars4, chars5, chars6, chars7, chars8] | |
| def available(name, suffix = 'com'): | |
| try: | |
| response = urllib.urlopen('%s%s.%s' % (url, name, suffix)).read() | |
| except Exception: | |
| return 'uncheck' | |
| fb = re.findall('Domain name is available', response) | |
| return fb and fb[0] or 'exist' | |
| def linecount(fname): | |
| count = 0 | |
| if os.path.isfile(fname): | |
| with open(fname) as f: | |
| for line in f.xreadlines(): count += 1 | |
| return count | |
| def check(chars, unf = UN): | |
| with open(AV, 'a') as f: | |
| with open(unf, 'a') as u: | |
| for x in chars: | |
| resp = available(x) | |
| if resp: | |
| if resp == 'uncheck': | |
| u.write(x) | |
| u.write('\n') | |
| elif resp != 'exist': | |
| f.write(x) | |
| f.write('\n') | |
| f.flush() | |
| u.flush() | |
| print 'finish!' | |
| print '''summary: | |
| available: %d | |
| uncheck: %d | |
| ''' % (linecount(AV), linecount(unf)) | |
| def recheck(fn = UN): | |
| lt = [] | |
| with open(UN) as f: | |
| while True: | |
| line = f.readline() | |
| if not line: | |
| break | |
| lt.append(line) | |
| if lt: | |
| check(lt, unf = 'uncheck.tmp.txt') | |
| def main(): | |
| if len(sys.argv)>1 and sys.argv[1].isdigit(): | |
| num = int(sys.argv[1])-1 | |
| if num in range(8): | |
| check(chars[num]) | |
| else: | |
| print 'out range' | |
| else: | |
| print 'args error' | |
| if __name__ == '__main__': | |
| # main() | |
| # recheck() | |
| print available(sys.argv[1]) or False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment