发布时间:2019-09-12 07:55:01编辑:auto阅读(2671)
一.说明
Python的Socket模块提供有域名转为对应IP地址的方法。本例中,将urllist.txt中的每行URL都试图解析成IP地址,保存到iplist.txt。需要注的是,socket.gethostbyname(url)方法中的url参数不能带有“http”这样的协议前缀,否则不能解析成IP地址。为了对比解析效果,加入了两条错误的域名格式,以便引起读者注意。
二.代码
#coding:utf-8
#build by LandGrey
#2016-03-10
import socket
def URL2IP():
for oneurl in urllist.readlines():
url=str(oneurl.strip())[7:]
print url
try:
ip =socket.gethostbyname(url)
print ip
iplist.writelines(str(ip)+"\n")
except:
print "this URL 2 IP ERROR "
try:
urllist=open("D:\urllist.txt","r")
iplist=open("D:\iplist.txt","w")
URL2IP()
urllist.close()
iplist.close()
print "complete !"
except:
print "ERROR !"
三.urllist
三.程序回显
sdk.mobcent.com
103.235.239.10
www.baidu.com
180.97.33.107
os-android.liqucn.com
116.90.87.156
vgo.21cn.com/portal/index.do
this URL 2 IP ERROR
you do 110…
this URL 2 IP ERROR
sj.good321.net
121.46.3.148
good.cn
23.27.192.117
complete !
四.最终结果
上一篇: Python3学习笔记01-Python
下一篇: python简单的多人聊天室
48926
48043
38808
35928
30347
27125
26129
20961
20789
19142
665°
744°
717°
715°
693°
649°
749°
833°
945°
1181°