一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Linux下Python獲取IP地址的代碼

Linux下Python獲取IP地址的代碼

2020-05-16 14:52腳本之家 Python

這篇文章主要介紹了Linux下Python獲取IP地址的代碼,需要的朋友可以參考下

《lnmp一鍵安裝包》中需要獲取ip地址,有2種情況:如果服務器只有私網地址沒有公網地址,這個時候獲取的IP(即私網地址)不能用來判斷服務器的位置,于是取其網關地址用來判斷服務器在國內還是國外(腳本為了使國內用戶快速下載,yum源自動設置成163,這個情況就需要獲取網關地址);如果服務器有公網地址,這時獲取的IP地址可用來直接判斷服務器地理位置。

獲取服務器IP,如果有公網地址就取公網地址,沒有公網地址就取私網網址
下面是之前我用shell來獲取本地IP腳本:

IP=`ifconfig | grep 'inet addr:' | cut -d: -f2 | grep -v ^10\. | grep -v ^192\.168 | grep -v ^172\. | \
grep -v ^127\. | awk '{print $1}' | awk '{print;exit}'`
 
[ ! -n "$IP" ] && IP=`ifconfig | grep 'inet addr:' | cut -d: -f2 | grep -v ^127\. | \
awk '{print $1}' | awk '{print;exit}'`

Python版:get_local_ip.py:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
import socket
def Get_local_ip():
 """
 Returns the actual ip of the local machine.
 This code figures out what source address would be used if some traffic
 were to be sent out to some well known address on the Internet. In this
 case, a Google DNS server is used, but the specific address does not
 matter much. No traffic is actually sent.
 """
 try:
  csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  csock.connect(('8.8.8.8', 80))
  (addr, port) = csock.getsockname()
  csock.close()
  return addr
 except socket.error:
  return "127.0.0.1"
 
if __name__ == "__main__":
 local_IP = Get_local_ip()
 print local_IP

有公網地址直接獲取,沒有公網地址就獲取網關地址(用于判斷IP地址的地理位置):get_public_ip.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
import re,urllib2
class Get_public_ip:
 def getip(self):
  try:
   myip = self.visit("http://www.whereismyip.com/")
  except:
   try:
    myip = self.visit("http://www.ip138.com/ip2city.asp")
   except:
    myip = "So sorry!!!"
  return myip
 def visit(self,url):
  opener = urllib2.urlopen(url)
  if url == opener.geturl():
   str = opener.read()
  return re.search('\d+\.\d+\.\d+\.\d+',str).group(0)
 
if __name__ == "__main__":
 getmyip = Get_public_ip()
 print getmyip.getip()

判斷服務器IP地理位置: get_ip_area.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env python
#coding:utf-8
try:
 import sys,urllib2,json
 apiurl = "http://ip.taobao.com/service/getIpInfo.php?ip=%s" % sys.argv[1]
 content = urllib2.urlopen(apiurl).read()
 data = json.loads(content)['data']
 code = json.loads(content)['code']
 if code == 0:
  print data['country_id']
 else:
  print data
except:
 print "Usage:%s IP" % sys.argv[0]

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲一区二区三区深夜天堂 | 男男调教打屁股 | 亚洲欧洲综合 | 网站色小妹 | 18无删减羞羞网站动漫 | kkkk4444在线看片免费 | 午夜国产在线 | 精品在线91 | 日本无翼乌漫画 | 欧美亚洲国产另类在线观看 | 午夜伦伦电影理论片费看 | 天天色综合6 | 国产激情影院 | 青草青草久热精品视频在线网站 | 手机在线观看精品国产片 | 成年男女免费视频观看性 | 高清在线观看mv的网址免费 | 午夜久久免影院欧洲 | t66y地址一地址二地址三 | 国产精品久久久久网站 | 99免费在线视频 | 欧美日韩精彩视频 | 99欧美精品 | 久久久伊人影院 | chaopeng在线观看 | 福利一区福利二区 | 欧美色fx性乌克兰 | 欧洲老太玩小伙 | 久久噜国产精品拍拍拍拍 | 精品在线播放 | a级亚洲片精品久久久久久久 | 国产成人久久精品区一区二区 | 成人夜视频寂寞在线观看 | 特黄视频 | 亚洲精品电影天堂网 | 亚欧国产 | 四虎在线播放 | 亚洲无线一二三区2021 | 动漫白丝袜美女羞羞 | 久久热在线视频精品店 | 婷婷久久综合 |