Python使用Socket获取实时whois信息

好吧,各语言没多大区别,因为经常根域名打交道,用python写个脚本自己用,原理就是whois查询服务器提供43端口查询信息,所以只需要要服务器建立socket链接,发送数据即可。

功能

1.批量查询域名注册状态;

2.批量查询关键词;

3.查询单个域名whois信息;

使用方法

1.将要查询的所有域名/关键词写入文件”/the/path/to/data.txt”,”data.txt”每行一个域名,或者每行一个关键词;

2.执行脚本,按提示进行操作即可;

3.如果批量域名或关键词查询,将会在当前目录下生成查询结果。

python-whois

源码

请原谅我不注释的良好习惯,好吧其实是太简单,错误信息都存储在字典里,注释反而画蛇添足。

 

# -*- coding: utf-8 -*-

def get_server(suffix = ''):
    suffixs = {
        'com': 'whois.internic.net',
        'net': 'whois.internic.net',
        'cn': 'whois.cnnic.net.cn',
        'cc': 'whois.nic.cc'
    }
    if suffixs.has_key(suffix):
        return suffixs[suffix]
    return ''

def get_suffix(domain = ''):
    domain = domain.strip().lower()
    domainSplit = domain.split('.', 1)
    if len(domainSplit) == 2:
        return domainSplit[1]
    return ''

def get_whois(domain = '', timeout = 1):
    data = {'success': 0, 'code': 0, 'info': ''}
    suffix = get_suffix(domain)
    if not suffix:
        data['code'] = 400
        data['info'] = 'Not a domain'
        return data
    server = get_server(suffix)
    if not server:
        data['code'] = 401
        data['info'] = 'Not find a server'
        return data

    import socket
    socket.setdefaulttimeout(timeout)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    try:
        s.connect((server, 43))
    except:
        s.close()
        data['code'] = 500
        data['info'] = 'Can not connect to the server'
        return data
    s.send(domain + 'rn')
    info = ''
    while 1:
        try:
            res = s.recv(1024)
        except:
            s.close()
            data['code'] = 501
            data['info'] = 'Connect to the server timeout'
            return data
        if not res:
            break
        else:
            info += res
    s.close()
    data['success'] = 1
    data['code'] = 200
    data['info'] = info
    return data

def get_reginfo(domain = ''):
    data = get_whois(domain)
    if not data['success']:
        return data
    suffix = get_suffix(domain)
    if suffix == 'cn':
        pattern = 'no matching record.'
    else:
        pattern = 'No match for '

    if data['info'].find(pattern) >= 0:
        data['info'] = 'Not be registered'
    else:
        data['code'] = 201
        data['info'] = 'Has be registered'

    return data


def input_command():
    try:
        cmd = raw_input('>')
    except KeyboardInterrupt:
        return ''
    return cmd

def input_filesource():
    try:
        filesource = raw_input('>')
    except KeyboardInterrupt:
        return ''
    if not filesource:
        filesource = './data.txt'
    return filesource

def input_suffix():
    try:
        suffix = raw_input('>')
    except KeyboardInterrupt:
        return ''
    if not suffix:
        suffix = 'com'
    return suffix

def input_domain():
    try:
        domain = raw_input('>')
    except KeyboardInterrupt:
        return ''
    return domain


if __name__ == '__main__':
    import sys, os
    print 'Choose: 1.Domains 2.Keywords  3.Whois'
    cmd = input_command()
    if cmd == '1':
        suffix = ''
    elif cmd == '2':
        print 'Enter suffix for Keywords, leave empty will use "com"'
        while 1:
            suffix = input_suffix()
            if get_server(suffix):
                suffix = '.' + suffix
                break
            else:
                print 'Inviable suffix'
            if not suffix:
                sys.exit(0)
    elif cmd == '3':
        print 'Enter a domain to get the whois info'
        while 1:
            domain = input_domain()
            if not domain:
                sys.exit(0)
            whois = get_whois(domain)
            print whois['info'].decode('utf-8').encode(sys.getfilesystemencoding())
    else:
        print 'Not a valiable command'
        sys.exit(0)
    print 'Enter the path of data file, leave empty will use "./data.txt"'
    while 1:
        filesource = input_filesource()
        if not filesource:
            sys.exit(0)
        if os.path.isfile(filesource):
            break
        else:
            print 'File not exisits'

    filesuccess = './_ok.txt'
    fileerror = './_error.txt'
    fsource = open(filesource, 'r')
    fsuccess = open(filesuccess, 'w')
    ferror = open(fileerror, 'w')

    for line in fsource:
        line = line.strip()
        if not line:
            continue
        domain = line + suffix
        reginfo = get_reginfo(domain)
        if reginfo['success'] and reginfo['code'] == 200:
            fsuccess.write(domain + 'n')
        elif not reginfo['success']:
            ferror.write(domain + ': ' + reginfo['info'] + 'n')
        else:
            continue

    fsource.close()
    fsuccess.close()
    ferror.close()

    print 'Done! Check "_ok.txt" "_error.txt" for detail. Press Enter to exit.'

    try:
        raw_input()
    except KeyboardInterrupt:
        sys.exit(0)