Use Python for WHOIS queries and database

Source: Internet
Author: User
Tags dnssec
Whois information will be obtained from the WHOIS query and stored in the database specific methods But more elaborate write the domain name or match the offending domain name to the database where the request occurred
"'--------------------------ver:2.0 date:2017/11/22 auth:wud---------------------- ----' Import DNS import mysqldb import whois import sys from time import ctime def getwhois (ur
        L): try:data = Whois.whois (URL) print data print "Whois query successfully!"
        Return data except:print "whois query fail!"
        F2 = open ("Fail.txt", ' r+ ') print >> f2, url f2.close () Pass def getip (URL): try: query = Sys.argv[0] DNS. Discovernameservers () Reqobj = DNS. Request (URL) answerobj = Reqobj.req (Name=query, Qtype=dns. TYPE.A) if not Len (answerobj.answers): Return to item in ANSWEROBJ.ANSWERS:IP = ("%s")% (item[' data ']) print "IP is:", IP return IP except:print "Time Out" F2 = Op En ("fail.txt", ' r+ ') print >> f2, url F2.cloSE () Pass def whoisoperation (ip,url,data): Text = data Try:updated_date = str (text[' updated_date '] status = STR (text[' status ']) Whoisname = str (text[' registrant_name ')) dnssec = str (text[' Dnsse C '] city = str (text[' tech_city ']) expiration_date = str (text[' expiration_date ']) zipcode = str (te
        xt[' ZipCode ']) domain_name = str (text[' domain_name ']) country = str (text[' registrant_state_province '))
        Whois_server = str (text[' whois_server ') state = str (text[' state ')) phone = str (text[' tech_phone ']) Registrar = str (text[' Registrar ']) # referral = text[' referral '] referral_url = str (text[' Referral_  URL ']) address = str (text[' registrant_address ']) name_servers = str (text[' name_servers ']) emails = STR (text[' emails ']) creation_date = str (text[' creation_date ']) try:print "Connecting Databas Es ... "db =MySQLdb.connect ("xxx.xxx.xxx.xxx", "xxxxx", "xxxxxxxxx", "xxx", charset= "UTF8") print "conncected!" cursor = Db.cursor () cursor.execute (' INSERT into Whois_info (url,ip,updated_date,status,whois_name,dnssec,ci Ty,expiration_date,zipcode,domain_name,country,whois_server,state,registrar,referral_url,address,name_servers,
                           Creation_date,emails,insert_time] Values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) '. [Url,ip,updated_date,status,whoisname,dnssec,city,expiration_date,zipcode,domain_name,country,w
            Hois_server, State,registrar,referral_url,address,name_servers,creation_date,emails,ctime ()])
        Db.commit () print "Insert successfully!" Except:print "Connect MySQL failed" F2 = open ("Fail.txt", ' r+ ') print >> f2, ur L f2.close () Pass try:updated_date =Str (text[' updated_date ']) status = STR (text[' status ') Whoisname = str (text[' tech_name ']) DNSSEC = str (text[' dnssec ') city = str (text[' city ']) expiration_date = St R (text[' Expiration_date ']) zipcode = str (text[' zipcode ']) domain_name = str (text[' Domain_n
                Ame ']) country = str (text[' country ']) Whois_server = str (text[' whois_server ')) State = str (text[' state ']) Registrar = str (text[' Registrar ')) # referral = text[' Refe
                Rral '] Referral_url = str (text[' Referral_url ']) address = str (text[' address ')  Name_servers = str (text[' name_servers ']) emails = str (text[' emails ']) Creation_date = Str (text[' creation_date ']) try:print "Connecting databases ..." D b = MySQLdb.connect ("xxx. Xxx.xxx.xxx "," xxxxx "," xxxxxxxxx "," xxx ", charset=" UTF8 ") print" conncected! " cursor = Db.cursor () cursor.execute (' INSERT into Whois_info (url,ip,updated_da Te,status,whois_name,dnssec,city,expiration_date,zipcode,domain_name,country,whois_server,state,registrar,  Referral_url,address,name_servers,creation_date,emails,insert_time values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s) ', [URL, IP, updated_date, status, Whoisname, DNSSEC, City, Expira Tion_date, ZipCode, domain_name, Country, Whois_server, State, Registrar, Referral_url, address, Name_servers, creation_date, emails, CTime ()]) db.commit () print Insert Successfu
                lly! "
                    Except:print "Connect MySQL failed" F2 = open ("Fail.txt", ' r+ ') Print >> F2, URL f2.close () except:f2 = open ("Fail.txt", ' r+ ') print >> f2, url
        F2.close () print "Mathc fail!"
        Pass def Main (): F = open ("Fail.txt", ' r ') f1 = open ("Whois.txt", ' r+ ') flag = 4 while (flag>0):
        url = f.readline () [: -1] print 5-flag print URL ip = getip (URL) data = Getwhois (URL)
        Whoisoperation (ip,url,data) print >>f1, url print >>f1, IP print >>f1, data
 flag-=1 if __name__ = = ' __main__ ': Main ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.