Python多線程檢查網域名稱是否存在

來源:互聯網
上載者:User

個人想查看下哪些短的網域名稱還沒有被用來提供網路服務, 自己可從中選擇一些有趣的使用. 

首先使用字典產生演算法, 產生一個關鍵字列表檔案. 如下

#!/usr/bin/env python#coding=utf-8"""filename: make_key.pyauthor: zz_ddate: 2013-05-25"""import sysimport getoptlen_ch_range = 0def mk_key(plen, cnt, pstr):    if cnt < plen:        for i in range(len_ch_range):            mk_key(plen, cnt+1, pstr+ch_range[i])    else:        for i in range(len_ch_range):            print pstr+ch_range[i]def help():    print '%s -n len [-h]' % sys.argv[0]if __name__ == "__main__":        plen = 0    try:        opts, args = getopt.getopt(sys.argv[1:], "n:h")    except getopt.GetoptError as err:        print str(err)        help()        sys.exit(2)    for o, a in opts:        if o == '-n':            if int(a) > 0:                plen = int(a)        elif o == '-h':            help()            sys.exit(0)    if plen <= 0:        help()        sys.exit(0)        ch_range = 'abcdefghijklmnopqrstuvwxyz0123456789'    len_ch_range = len(ch_range)    mk_key(plen, 1, '')            

取得關鍵字 key, 分別檢查 www.key.com 和 www.key.net 是否存在, 用 domain_key 表示.

檢查網域名稱的步驟是:

1. 擷取網域名稱不存在時電信或網通的劫持IP;

2.如果 gethostname(domain_key) 出錯或者返回劫持的 IP, 則此 domain_key 未提供服務;

代碼如下:

#!/usr/bin/env python#coding=utf-8"""filename: check_domain.pyauthor: zz_ddate: 2013-05-25"""import sysimport timeimport threadingimport getoptimport socketbsema = None# 通過一個不存在的網域名稱, 來擷取網域名稱不存在時被 isp 劫持到的 ipno_exist_domain = 'www.djflskfjiosfhossdfdsfsfsdfdi.com'fake_ip = None  def check_domain(key):    '''    check if a domain is exist     '''    try:        domain = 'www.%s.com' % key        ip = socket.gethostbyname(domain)        if ip == fake_ip:            print domain    except socket.gaierror:        print domain    except socket.timeout:        print domain    except socket.error:        print domain            try:        domain = 'www.%s.net' % key        ip = socket.gethostbyname(domain)        if ip == fake_ip:            print domain    except socket.gaierror:        print domain    except socket.timeout:        print domain    except socket.error:        print domain            bsema.release()def help():    print '%s -f keyfile [-t threads_num] [-h]' % sys.argv[0]if __name__ == "__main__":    file_keys = None    thread_num = 5    try:        opts, args = getopt.getopt(sys.argv[1:], "f:t:h")    except getopt.GetoptError as err:        print str(err)        help()        sys.exit(2)    for o, a in opts:        if o == '-f':            file_keys = a        elif o == '-t' and int(a) > 0:            thread_num = int(a)        elif o == '-h':            help()            sys.exit(0)                if not file_keys:        help()        sys.exit(-2)        try:        fake_ip = socket.gethostbyname(no_exist_domain)    except socket.error:        fake_ip = None            cnt = 0    socket.setdefaulttimeout(10)    bsema = threading.BoundedSemaphore(thread_num)    fs = file(file_keys, 'r')    key = fs.readline()    while key:        key = key.strip()        if key:            bsema.acquire()            th = threading.Thread(target=check_domain, args=(key,))            th.start()            #check_domain(key)            cnt += 1        key = fs.readline()        if cnt >= 10:            cnt = 0            fake_ip = socket.gethostbyname(no_exist_domain)    

手動檢查了幾個輸出的結果, 並無異常. 還是有好一些有趣的網域名稱沒有被註冊的.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.