日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Linux下Python獲取IP地址的代碼

系統(tǒng) 1664 0

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

獲取服務(wù)器IP,如果有公網(wǎng)地址就取公網(wǎng)地址,沒有公網(wǎng)地址就取私網(wǎng)網(wǎng)址
下面是之前我用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:

          
#!/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
        

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

          
#!/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()
        

判斷服務(wù)器IP地理位置: get_ip_area.py

          
#!/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]
        

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 垣曲县| 甘洛县| 三江| 芮城县| 江油市| 昭通市| 安国市| 怀安县| 陆良县| 措美县| 长沙县| 定边县| 蕉岭县| 健康| 南澳县| 黄浦区| 五大连池市| 卢氏县| 昂仁县| 景宁| 怀远县| 淳化县| 宝山区| 贵溪市| 云阳县| 滦平县| 襄汾县| 潍坊市| 华池县| 奎屯市| 大冶市| 开远市| 林西县| 黄龙县| 土默特左旗| 凤阳县| 清河县| 区。| 新蔡县| 滕州市| 海淀区|