发布时间:2019-09-12 07:55:01编辑:auto阅读(2839)
一.说明 
  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简单的多人聊天室
 51194
 50609
 41233
 38051
 32513
 29420
 28281
 23136
 23094
 21432
 1489°
 2202°
 1825°
 1753°
 2064°
 1813°
 2499°
 4193°
 4055°
 2894°