【python】获取本机的hostname以及外网ip
2013-03-19 15:05
447 查看
1 得到外网IP地址
import re,urllib2
class Getmyip:
def
getip(self):
try:
myip
= self.visit("http://www.ip138.com/ip2city.asp")
return
myip
except:
try:
myip
= self.visit("http://www.bliao.com/ip.phtml")
except:
try:
myip
= self.visit("http://www.whereismyip.com/")
except:
myip
= "So sorry!!!"
return
myip
def
visit(self,url):
opener
= urllib2.urlopen(url)
if
url == opener.geturl():
str
= opener.read()
asd=re.search('\d+\.\d+\.\d+\.\d+',str).group(0)
return
asd
getmyip
= Getmyip()
localip = getmyip.getip()
print localip
2 获取本地IP
2.1 windows和linux下
import
socket
localIP=socket.gethostbyname(socket.gethostname())
print "local ip:%s "%localIP
ipList=socket.gethostbyname_ex(socket.gethostname())
for i in ipList:
if
i!=localIP:
print
"external IP:%s"%i
2.2 linux下
import
socket,fcntl,struct
def get_ip_address(ifname):
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
return
socket.inet_ntoa(fcntl.ioctl(
import re,urllib2
class Getmyip:
def
getip(self):
try:
myip
= self.visit("http://www.ip138.com/ip2city.asp")
return
myip
except:
try:
myip
= self.visit("http://www.bliao.com/ip.phtml")
except:
try:
myip
= self.visit("http://www.whereismyip.com/")
except:
myip
= "So sorry!!!"
return
myip
def
visit(self,url):
opener
= urllib2.urlopen(url)
if
url == opener.geturl():
str
= opener.read()
asd=re.search('\d+\.\d+\.\d+\.\d+',str).group(0)
return
asd
getmyip
= Getmyip()
localip = getmyip.getip()
print localip
2 获取本地IP
2.1 windows和linux下
import
socket
localIP=socket.gethostbyname(socket.gethostname())
print "local ip:%s "%localIP
ipList=socket.gethostbyname_ex(socket.gethostname())
for i in ipList:
if
i!=localIP:
"external IP:%s"%i
2.2 linux下
import
socket,fcntl,struct
def get_ip_address(ifname):
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
return
socket.inet_ntoa(fcntl.ioctl(
相关文章推荐
- 【python】获取本机的hostname以及外网ip
- python shell 根据ip 获取 hostname || 根据hostname 获取 ip
- python获取外网IP并发邮件
- Python获取外网IP
- Python获取本机外网IP
- python获取外网ip地址的方法总结
- .NET记录-获取外网IP以及判断该IP是属于网通还是电信
- Python简单获取自身外网IP的方法
- .NET记录-获取外网IP以及判断该IP是属于网通还是电信
- python获取外网ip的代码
- python获取本机外网ip的方法
- python获取外网IP
- python获取外网ip地址的方法总结
- Python 获取主机ip与hostname
- 通过www.ip138.com获取本机外网ip以及所在地理位置
- 使用Python获取网段IP个数以及地址清单
- 使用Python获取网段IP个数以及地址清单的方法
- 用Python获取自己的外网IP
- Python获取外网ip
- python获取外网IP并发邮件的实现方法