Python method of implementing WHOIS query function source code

Source: Internet
Author: User

I'm afraid a lot of friends like me,makewithpythonlanguageincrediblycan beImplementwhoisServer Queryfunction. belowI put the code and instructions to everyone to see, who need to be able to refer to. Originally want to directly from the WHOIS server query, but found to write the socket with 43 port link server, but some server address is not clear, and the query command seems to have changed so do not want to toss, think directly with Chinaz page implementation of a forget. The following code is operated under Win7, and the installation python3.2 test passes.

Python 3.2 ways to implement the WHOIS query function source code:

#-*-Coding:utf-8-*-

Import urllib.request

def str_cut (str,startsep,endsep):

Str1=str.split (STARTSEP) [1]

Str2=str1.split (ENDSEP) [0]

return str2

def whois (host):

Url= ' http://whois.chinaz.com/' +host

Data=urllib.request.urlopen (URL). Read ()

Data=data.decode ()

if data.find (' <div id= ' Whoisinfo "class=" Div_whois "> ') ==-1:

Data= "The domain name is not registered and can now be registered"

Else

Data=str_cut (data, ' <div id= ' Whoisinfo "class=" Div_whois "> ', ' </div> ')

Data=data.replace (' <br/> ', ' \\n ') [:-1]

return ' query domain: ' +host+ ' \\n ' + '-' *60+ ' \\n ' +data

def query ():

Host=input (' Please enter the domain name does not contain ' http://www. ';( q/q exit): ') [:-1]

#data =whois (Host)

#print (data)

Host=host.lower ()

if host = = ' q ':

Exit ()

Else

Data=whois (host)

Print (data)

Input (' \\n Enter ENTER to continue ... \\n ')

Query ()

Query ()

Considering that some friends are using Python2.7 version, using the above code will cause some errors, I modified a bit to run:

#-*-Coding:utf-8-*-

Import Urllib

def str_cut (STR,STARTSEP,ENDSEP):

Str1=str.split (STARTSEP) [1]

Str2=str1.split (ENDSEP) [0]

Return str2

def whois (host):

Url= ' http://whois.chinaz.com/' +host

Data=urllib.urlopen (URL). Read ()

If Data.find (' <div id= ' Whoisinfo "class=" Div_whois "> ') ==-1:

Data= "The domain name is not registered and can now be registered"

Else

Data=str_cut (data, ' <div id= ' Whoisinfo "class=" Div_whois "> ', ' </div> ')

Data=data.replace (' <br/> ', ' \\n ')

Return ' query domain: ' +host+ ' \\n ' + '-' *60+ ' \\n ' +data

def query ():

Host=raw_input (' Please enter the domain name does not contain ' http://www. ';( q/q exit): ')

Host=host.lower ()

If host = = ' Q ':

Exit ()

Else

Data=whois (host)

Print (data)

Raw_input (U ' input enter to continue ... \\n ')

Query ()

above is python implementation of the Whois query function method,Python3 and Python2 are given out, hoping to help everyone.

Python method of implementing WHOIS query function source code

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.