#!/root/.virtualenvs/script/bin/python

#coding=utf-8

import re

import time, mechanize

import urllib2

import random

URL = 'http://whois.chinaz.com/'

def str_cut(str,startsep,endsep):

    str1=str.split(startsep)[1]

    str2=str1.split(endsep)[0]

    return str2

br = mechanize.Browser()

br.set_handle_equiv(True)

br.set_handle_gzip(False)

br.set_handle_redirect(True)

br.set_handle_referer(True)

br.set_handle_robots(True)

br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

#br.set_debug_http(True)

#br.set_debug_redirects(True)

#br.set_debug_responses(True)

br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.

1')]

domain_list = [你的域名列表]

r = br.open(URL)

for index, item in enumerate(domain_list):

    try:

        br.select_form(nr=0)

        br.form['DomainName'] = item

        br.submit()

        contents = br.response().read()

        if contents.find('

')==-1:

            print item +  " " + "not found"

        else:

            contents=str_cut(contents,'

','
')

            contents=contents.replace('
',',')[:-1]

            a = re.split(',',contents)

            for i in a:

                m = re.match(r"过期时间", i)

                if m:

                    print '域名 : %s, 过期时间 : %s' % (item, re.sub(r"年|月", '-', i[15:-3]).strip()[:10])

        br.back()

    except Exception,e:

        print Exception,":",e

    time.sleep(1.27*random.randint(4, 12))