码迷,mamicode.com
首页 > 其他好文 > 详细

how to get the client's IP address

时间:2016-06-16 14:32:05      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

有时候总会有这种需求,今天我先起个头,汇总一下可以获取当前设备IP address的办法:

1. shell 正则 + python

    DATAIP = "ifconfig -a|awk \‘/(cast)/ {print $2}\‘|cut -f1-2 -d."
    data= subprocess.Popen(DATAIP,stdout=subprocess.PIPE,shell=True)
    tmp = data.stdout.read()
    _Real_IP = tmp.strip()

 2. python 本身的re 模块,但是这个我在生产中遇到过一个问题,在chrom OS 的command 下面 return的列表是空的,但是在 ubuntu的note book 上面run 是可以正常return的

import subprocess
import re

def _GetLine():
  ipstr = ([0-9]{1,3}\.){1}[0-9]{1,3}
  ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
  output = ipconfig_process.stdout.read()
    ip_pattern = re.compile((inet addr:%s) % ipstr)
  pattern = re.compile(ipstr)
  iplist = []
  for ipaddr in re.finditer(ip_pattern, str(output)):
      print type(ipaddr)
      ip = pattern.search(ipaddr.group())
      if ip.group() != "127.0":
          iplist.append(ip.group())

  return iplist

方法而也是在网上看到的,但是实际使用时却发现这个方法不work, 始终只能返回空的列表,后来才临时改成了方法1;方法二原始的博文还有判定platform的功能,可以自如的在windows 和 linux上面使用,但是链接没有记录,而且我只用到了他的一部分;

how to get the client's IP address

标签:

原文地址:http://www.cnblogs.com/winditsway/p/5590869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!