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

时间: 2024-10-28 18:58:40

how to get the client's IP address的相关文章

Get the client's IP address in socket.io

From: https://www.wentong.org/codex/question-2018081564702.html When using socket.IO in a Node.js server, is there an easy way to get the IP address of an incoming connection? I know you can get it from a standard HTTP connection, but socket.io is a

Linux Force DHCP Client (dhclient) to Renew IP Address

http://www.cyberciti.biz/faq/howto-linux-renew-dhcp-client-ip-address/‘m using Ubuntu Linux. How to force Linux to reacquire a new IP address from the DHCP server? What is the command in Linux equivalent to Windows’ “ipconfig /renew” command? You nee

IP address could not be resolved: Temporary failure in name resolution

今早发现mysql日志中有非常多例如以下的警告: 140724 18:41:25 [Warning] IP address '172.16.18.217' could not be resolved: Temporary failure in name resolution 140724 18:41:25 [Warning] IP address '172.16.18.217' could not be resolved: Temporary failure in name resolution

在windows下运行docker的问题【Error getting IP address: ***】

环境配置系统:windows 10docker:Docker Toolbox https://www.docker.com/products/docker-toolbox 问题描述windows下安装完Docker Toolbox后运行Docker Quickstart Terminal可能会看到如下错误 Creating Machine default... Running pre-create checks... Creating machine... Error creating mach

Java Regex match IP address

Reference: [1] https://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-expression/ import java.util.regex.Matcher; import java.util.regex.Pattern; public class IPAddressValidator{ private Pattern pattern; private Matcher ma

router 分配ip address(dhcp)和dhcp中继

路由器的主要功能是网络寻址,有时候也需要用来分配ip地址.但用路由器分配地址又会加大路由器的负载,所以,有的时候我们可以把分配地址的工作交给一台PC. 1.使用router分配ip address: Router>en Router#conf t Enter configuration commands, one per line.  End with CNTL/Z. Router(config)#int vlan 1 Router(config-if)#ip dhcp pool vlan1  

poj2105 IP Address(简单题)

题目链接:http://poj.org/problem?id=2105 Description Suppose you are reading byte streams from any device, representing IP addresses. Your task is to convert a 32 characters long sequence of '1s' and '0s' (bits) to a dotted decimal format. A dotted decima

Debian static ip address

1 // /etc/network/interfaces 2 auto eth0 3 iface eth0 inet static 4 address <Your ip address> 5 netmask <Your netmask> 6 gateway <Your gateway address> 7 broadcast <Your broadcast address> 8 9 // /etc/resolv.conf 10 nameserver <

If application data needs to be sent to IP address xx.xx.xx.xx, how does it work in underneath network?

This is to illustrate the communication between two separate machines which don't have a direct physical connection. Application data is generated and passed to layer 3 (network layer) which wrapps up the data with the destination IP address. Then ha