怎样编译mwhois

2018年7月1日 | 分类: 【技术】

【介绍】

m-whois是一个用Python开发的WHOIS查询模块。可以通过提供一个文件或者列表来找出尚未注册的域名。

要求:Python >= 2.7.3;暂不支持 Python 3。

代理:如果需要启用代理特性,需要安装 pysocks 模块。
下载:https://pypi.python.org/pypi/PySocks/

特性:
1. 直接连接WHOIS服务器查询单个/多个域名。
2. 仅输出尚未注册的域名。
3. 支持100多个后缀和30多个新后缀。
4. 支持如果首要WHOIS服务器被墙,可以备用WHOIS服务器(某些后缀的域名管理局是这样限制的)。
5. 通过代理连接WHOIS服务器。

计划特性:
1. 缓存查询结果以实现更快地响应,同时避免滥用域名管理局是的查询限制。
2. 输出查询结果为.CSV和.PDF文件。

官网:https://github.com/jrosco/mwhois

参考:https://sourceforge.net/projects/xwh0i5/
源码:https://sourceforge.net/projects/xwh0i5/files/source/

【安装】

git clone https://github.com/jrosco/mwhois.git mwhois && cd mwhois && python setup.py install

输出:

...
creating /usr/local/lib/python2.7/site-packages/mwhois
copying build/lib/mwhois/util.py -> /usr/local/lib/python2.7/site-packages/mwhois
...
byte-compiling /usr/local/lib/python2.7/site-packages/mwhois/util.py to util.pyc
...
running install_egg_info
Writing /usr/local/lib/python2.7/site-packages/mwhois-0.1.1b-py2.7.egg-info

【使用】

查询单个域名:

python examples/cmd.py github.com

输出:

   Domain Name: GITHUB.COM
   Registry Domain ID: 1264983250_DOMAIN_COM-VRSN
   Registrar WHOIS Server: whois.markmonitor.com
   Registrar URL: http://www.markmonitor.com
   Updated Date: 2017-06-26T16:02:39Z
   Creation Date: 2007-10-09T18:20:50Z
   Registry Expiry Date: 2020-10-09T18:20:50Z
   Registrar: MarkMonitor Inc.
...

查询多个域名:

数据:-t 后面的参数表示域名后缀;wordlist-sample中分行存储有多个关键词。

python examples/cmd.py -t com -i files/wordlist-sample

输出:

uucp.com                      Domain Taken
nuucp.com                     Domain Available
device.com                    Domain Taken
sysdiags.com                  Domain Available
...

图形界面:

使用默认的 TkInter 模块:

python examples/GUI/mgui.py

报错:

ImportError: No module named wx

参考:https://stackoverflow.com/questions/36540928/installing-wxpython-on-centos-7
参考:https://wiki.wxpython.org/How%20to%20install%20wxPython
下载:https://centos.pkgs.org/7/epel-x86_64/wxPython-2.8.12.0-9.el7.x86_64.rpm.html
下载:https://centos.pkgs.org/7/epel-x86_64/wxPython-devel-2.8.12.0-9.el7.x86_64.rpm.html

安装 wx :

yum install http://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/w/wxPython-2.8.12.0-9.el7.x86_64.rpm
yum install http://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/w/wxPython-devel-2.8.12.0-9.el7.x86_64.rpm

报错:

Unable to access the X Display, is $DISPLAY set properly?

参考:https://www.shiyanlou.com/questions/1875/
参考:http://landcareweb.com/questions/5509/ru-he-shi-yong-pylabyuan-cheng-bao-cun-shu-zi-zhong-fu
参考:https://blog.csdn.net/jiangxinyu/article/details/1630713
参考:https://github.com/kevinhughes27/TensorKart/issues/54

需要切换到桌面模式才可以运行!
使用X11的网络图形由客户端(远程)计算机工作,该计算机具有DISPLAY环境变量,该变量指示绘制图形的位置。

参考:https://www.jianshu.com/p/97478b613bbf

输入 startx 命令进入到图形桌面

输出:

-bash: startx: command not found

安装 x11 图形界面:

参考:https://ywnz.com/linuxjc/2228.html
参考:https://www.aclnz.com/interests/blogs/oracle-linux-startx-command-not-found-error
参考:https://zhoualine.iteye.com/blog/1806399
参考:https://www.jianshu.com/p/97478b613bbf
参考:http://amon.org/gui

yum groupinstall "X window system"
yum install xrdp
yum install tigervnc-server

关闭防火墙:

systemctl stop firewalld.service
systemctl disable firewalld.servie

查看SElinux状态:

sestatus 

临时关闭SElinux:

setenforce 0

永久关闭SElinux,编辑 /etc/selinux/config :

SELINUX=disabled

启动XRDP:

systemctl start xrdp
systemctl enable xrdp

测试连接:

使用Windows自带的远程桌面连接:
1. Windows键 + R -> 打开运行,输入 mstsc 命令,使用windows自带的远程桌面mstsc连接Centos 7.x远程桌面
2. 打开了远程桌面连接软件,输入ip地址,用户名:root;密码:root密码
3. 进入 Gnome 桌面

点击左上角 Applications 》 Terminal ,打开终端面板。

输入:

python examples/GUI/mgui.py

即可显示 mwhois 的 GUI 图形界面。

【打包】

生成Windows下的可执行文件:

参考:https://blog.csdn.net/whl826661099/article/details/79624326
参考:https://blog.csdn.net/heshao20/article/details/80957018

安装 Python :

列表:https://www.python.org/downloads/windows/
下载:https://www.python.org/ftp/python/2.7.16/python-2.7.16.amd64.msi

安装 pywin32 :

列表:https://sourceforge.net/projects/pywin32/files/pywin32/Build%20221/
下载:https://iweb.dl.sourceforge.net/project/pywin32/pywin32/Build%20221/pywin32-221.win-amd64-py2.7.exe

安装 pyinstaller :

列表:http://www.pyinstaller.org/downloads.html
下载:https://github.com/pyinstaller/pyinstaller/releases/download/v3.4/PyInstaller-3.4.tar.gz
版本:supports Python 2.7, 3.4–3.7
参考:https://blog.csdn.net/SuperChanon/article/details/8255566

下载并解压后和python放在相同的目录。

Windows键 + R -> 打开运行,输入 cmd 命令,打开Windows自带的终端。

在CMD命令行中进入pyinstaller目录:

d:
cd Python27\PyInstaller-3.4
python setup.py install

如果报错:

'python' is not recognized as an internal or external command, operable program or batch file.

参考:https://www.pythoncentral.io/add-python-to-path-python-is-not-recognized-as-an-internal-or-external-command/
参考:https://docs.python.org/2/faq/windows.html

Python2.7:This PC 》 Properties 》 Advanced system settings 》 Advanced 》 Environment variables 》 System variables 》 Find the PATH variable and click Edit
Python3.7:只需要重新安装Python程序,勾选“Add Python to environment variables”即可。

安装正常,返回:

...
Using d:\python37\lib\site-packages
Finished processing dependencies for PyInstaller==3.4

下载 mwhois 并解压后,放在 PyInstaller-3.4 目录下 :
下载:https://github.com/jrosco/mwhois/archive/master.zip

在CMD命令行中进入 PyInstaller-3.4 目录:

d:
cd Python27\PyInstaller-3.4
python pyinstaller.py -F mwhois\examples\GUI\mgui.py --noconsole

然后去双击执行这个 mgui.exe ,却无法打开,弹出窗口:

Failed to execute script mgui

参考:https://blog.csdn.net/wuminminmin/article/details/81166449 支持debug方法
参考:https://blog.csdn.net/A807296772/article/details/82769835

进行exe打包,得到一个目录:

python pyinstaller.py -D mwhois\examples\GUI\mgui.py

通过命令行执行.exe文件:

cd D:\Python27\PyInstaller-3.4\mgui\dist\mgui
mgui.exe

提示:

Traceback (most recent call last):
  File "mwhois\examples\GUI\mgui.py", line 3, in <module>
  File "D:\Python27\PyInstaller-3.4\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
    exec(bytecode, module.__dict__)
  File "mwhois\examples\GUI\mthread.py", line 4, in <module>
ImportError: No module named mwhois.whosearch
[12604] Failed to execute script mgui

如果使用 Python 3.7 来编译:

报错:

python 2640 warning lib notfound
...

参考:https://stackoverflow.com/questions/48712154/pyinstaller-warning-lib-not-found

this is due to pyinstaller 32 bit python
You can also run this before PyInstaller

set PATH=%PATH%;C:\Windows\System32\downlevel;

继续编译,这次没有缺少库的报错了。但出现Python语法错误:

Syntax error in D:\Python37\PyInstaller-3.4\mwhois\examples\GUI\mgui.py
  File "D:\Python37\PyInstaller-3.4\mwhois\examples\GUI\mgui.py", line 295
     except Exception, e:
                     ^
 SyntaxError: invalid syntax

参考:https://stackoverflow.com/questions/41596810/how-to-print-an-exception-in-python-3/41597017

问题来自Python升级到3之后的变化。

修改:

        except Exception, e:
            print e

为:

        except Exception as e:
            print(e)

这次正常编译成功:

...
5594 INFO: Appending archive to EXE D:\Python37\PyInstaller-3.4\mgui\dist\mgui.exe
5594 INFO: Building EXE from EXE-00.toc completed successfully.

参考:https://blog.csdn.net/wuminminmin/article/details/81166449 支持debug方法
参考:https://blog.csdn.net/A807296772/article/details/82769835

python pyinstaller.py -D mwhois\examples\GUI\mgui.py

进行exe打包,得到一个目录文件,通过命令行执行.exe文件:

cd D:\Python27\PyInstaller-3.4\mgui\dist\mgui
mgui.exe

提示:

Traceback (most recent call last):
  File "mwhois\examples\GUI\mgui.py", line 3, in <module>
ModuleNotFoundError: No module named 'mthread'

原来还有一个 mthread.py 尚未编译。

https://blog.csdn.net/wangmingfan123/article/details/78046893
https://blog.csdn.net/qq_42063091/article/details/82423221
https://blog.csdn.net/A807296772/article/details/82769835
https://blog.csdn.net/heshao20/article/details/80957018

生成Gnome下的可执行文件:

参考:https://blog.csdn.net/zengxiantao1994/article/details/76578421

安装 pyinstaller :

pip install --upgrade pip
pip install pyinstaller

报错:

ERROR: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-kH0ZbZ/future/

参考:https://github.com/facebook/prophet/issues/418

pip install --upgrade setuptools

然后安装成功。

检测版本:

pyinstaller --version

输出:

3.4

进入 Gnome 桌面,打开 Terminal ,输入:

pyinstaller -F examples/GUI/mgui.py --noconsole

然后会看到新增加了两个目录build和dist,dist下面的文件就是可以发布的可执行文件。
加 -F 参数让dist下面只有一个可执行文件,这个单文件就可以发布了,可以运行在你正在使用的操作系统类似的系统的下面。

【代码】

获得指定域名的WHOIS服务器:

from mwhois.whois import WhoisInfo
w = WhoisInfo()
w.domain = "on.net"
whoisserver = w.get_whois_server()
print(whoisserver)

输出:

whois.internic.net

从WHOIS服务器返回指定域名用于指示’not found’的字符串:

from mwhois.whois import WhoisInfo
w2 = WhoisInfo()
w2.domain = "ping.net"
w2.get_domain_tld()
txt = w2.tld_not_found_text()
print(txt)

输出:

No match for

从WHOIS服务器返回指定域名后缀用于指示’not found’的字符串:

from mwhois.whois import WhoisInfo
w3 = WhoisInfo()
w3.tld = "jp"
txt2 = w3.tld_not_found_text()
print(txt2)

输出:

No match!!

单域名查询,输出原始WHOIS数据(设置 s.connection.proxy = true 以设置代理):

from mwhois.whosearch import WhoisSearch
import mwhois.const as CONST
s = WhoisSearch(dname='sohu.com', debug=False)
s.connection.proxy = False
s.connection.proxy_host = '127.0.0.1'
s.connection.proxy_port = 8080
s.connection.proxy_type = CONST.PROXY_TYPE_SOCKS5
s.whois_search()
print(s.response())

输出:

Domain Name: cnnic.cn
ROID: 20030310s10001s00012956-cn
Domain Status: serverDeleteProhibited
Domain Status: serverUpdateProhibited
Domain Status: serverTransferProhibited
Registrant ID: s1255673574881
Registrant: 中国互联网络信息中心
Registrant Contact Email: servicei@cnnic.cn
...

单域名查询,输出WHOIS数据的元数据(设置 s.connection.proxy = true 以设置代理):

from mwhois.whosearch import WhoisSearch
import mwhois.const as CONST
s = WhoisSearch(dname='sohu.com', debug=False)
s.connection.proxy = False
s.connection.proxy_host = '127.0.0.1'
s.connection.proxy_port = 8080
s.connection.proxy_type = CONST.PROXY_TYPE_SOCKS5
s.whois_search()
print(s.creation_date())
print(s.expiry_date())
print(s.update_date())
print(s.registrant())
print(s.nameservers())
print(s.emails())

输出:

1998-7-5
N/A
2017-10-30
[]
[' NS11.SOHU.COM\r', ' NS12.SOHU.COM\r', ' NS13.SOHU.COM\r', ' NS14.SOHU.COM\r', ' NS15.SOHU.COM\r', ' NS16.SOHU.COM\r']
[]

多域名查询(设置 deadonly=False 以显示所有域名状态):

from mwhois.whosearch import WhoisSearch
domain_list = ['google','doesnotexist123','yahoo']
m = WhoisSearch(tld='org',wordlist=domain_list, deadonly=False)
multi = m.whois_multi_search()
"""Returns True(1) if found or False(0) is not found"""
for i in multi:
    print i

输出:

[1, 'google.org']
[0, 'doesnotexist123.org']
[1, 'yahoo.org']

通过文件导入进行多域名查询,输出域名状态:

from mwhois.whosearch import WhoisSearch
m = WhoisSearch(debug=False)
m.tld='com'
m.wordlist='./wordlist.txt'
txt_file = m.whois_multi_search()
""" Loop over txt_file generator and print list results containing ['status value', 'domain name']""" 
for x in txt_file:
    domain = x[1]
    if x[0] == 1:
        status = 'taken'
    else:
        status = 'not taken'
    print('Domain %s is %s ' % (domain, status))

输出:

Domain google.com is taken
...
Domain rewrvdfgrefdvf.com is not taken
Domain fgfdbfdvfdvffv.com is not taken
Domain devadmin.com is taken

通过文件导入进行多域名查询,输出域名属性:

from mwhois.whosearch import WhoisSearch
m = WhoisSearch(debug=False)
m.tld='com'
m.wordlist='./wordlist.txt'
txt_file = m.whois_multi_search()
""" Loop over txt_file generator and print whois attributes"""
for x in txt_file:
    domain = x[1]
    try: 
        date = m.creation_date()[0]
    except:
        date = 'N/A'
    print('Domain %s has creation date %s' % (domain, date))

输出:

Domain phield.com has creation date 2
Domain sohu.com has creation date 1
Domain ebookle.com has creation date 2