Python 官方文档:入门教程 => 点击学习
一.说明 python的Socket模块提供有域名转为对应IP地址的方法。本例中,将urllist.txt中的每行URL都试图解析成IP地址,保存到iplist.txt。需要注的是,socket.gethostbyname(
一.说明
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 !
四.最终结果
--结束END--
本文标题: Python——域名解析成IP地址
本文链接: https://www.lsjlt.com/news/189385.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0