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