how to get the client's IP address

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了how to get the client's IP address相关的知识,希望对你有一定的参考价值。

有时候总会有这种需求,今天我先起个头,汇总一下可以获取当前设备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的主要内容,如果未能解决你的问题,请参考以下文章

How to get the edited text from itext in fabricjs

How to get gradients with respect to the inputs in pytorch

How to get the edited text from itext in fabricjs

How to get the date N days ago in Python

How to Get the Length of File in C

huggingface | how to get the params‘ number of a model