域名解析正确性校验脚本(python多进程)

来源:互联网 发布:越南手机网络制式 编辑:程序博客网 时间:2024/05/21 22:46

本人原创,转载请注明出处,合法转载

文章出处:http://blog.csdn.net/yanshu2012

#!/bin/env python# coding=UTF-8import socketimport sysimport redisfrom multiprocessing import Poolimport timeredis_host="127.0.0.1"redis_port=6379all_key="all"right_key="right"g_progress_num=150def check_domain(domain):try:host=domain.split("\n")[0]#print host   results=socket.getaddrinfo(host,80, 0, 0, socket.IPPROTO_TCP)#print results#print results[0][4]return Trueexcept Exception as e:                #print "[ERROR]%s" % (e)                 #print "[ERROR]%s" % (line) return Falsedef work_progress():try:cli=redis.StrictRedis(redis_host,redis_port,db=0)exit_count=0while exit_count < 3:domain=redis_cli.lpop(all_key)#print domainif domain is not None:if check_domain(domain)== True:redis_cli.rpush(right_key,domain)else:exit_count += 1time.sleep(1)except Exception as e:                print "[Exception:work_progress]%s" % (e) print "Start time:%s" % (time.ctime())redis_cli=redis.StrictRedis(redis_host,redis_port,db=0)for line in sys.stdin:try:line=line.split("\n")[0]#print lineredis_cli.rpush(all_key,line)except Exception as e:print "[Exception:stdin]%s" % (e) p = Pool(g_progress_num)for i in xrange(g_progress_num): try:  p.apply_async(work_progress) except Exception as e:print "[Exception:progress]%s" % (e)p.close()p.join()log_file=open("right_domain","a")log_count=0while log_count < 3:     domain=redis_cli.lpop(right_key)     if domain is not None:  domain += "\n"  log_file.write(domain)     else:          log_count += 1          time.sleep(1)print "End time:%s" % (time.ctime())



0 0