python get local ip and sth. others
来源:互联网 发布:全国土建造价软件 编辑:程序博客网 时间:2024/06/05 19:40
import socket
import fcntl
import struct
def get_ip_address(ifname):
skt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print skt
pktString = fcntl.ioctl(skt.fileno(), 0x8915, struct.pack('256s', ifname[:15]))
print pktString
ipString = socket.inet_ntoa(pktString[20:24])
print ipString
return ipString
print get_ip_address('lo')
print get_ip_address('eth1')
Windows:
import re,urllib2
from subprocess import Popen, PIPE
print "本机的私网IP地址为:" + re.search('\d+\.\d+\.\d+\.\d+',Popen('ipconfig', stdout=PIPE).stdout.read()).group(0)
print "本机的公网IP地址为:" + re.search('\d+\.\d+\.\d+\.\d+',urllib2.urlopen("http://www.7qiao.cn/ip.php").read()).group(0)
===========================
获取本机IP和Port,远程IP和Port
# -*- coding: cp936 -*-
"""
使用socket获取更多的信息
getsocketname:获得本机的信息(IP和port)
getpeername:获得远程机器的信息(IP和port)
fileno:每一个socket对应一个fd,使用此方法可以获得fd,为一个整数
"""
import socket</code>
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
port = socket.getservbyname("http","tcp")
s.connect(("www.google.com",port))
print "Connect from",s.getsockname()
print "Connect to",s.getpeername()
print "file descriptor",s.fileno()
s.close()
直接获取远程IP
import socket
socket.gethostbyname('google.com')
直接获取本地IP
import socket
socket.gethostbyname(socket.gethostname())
socket.gethostbyname_ex(socket.gethostname())
- python get local ip and sth. others
- Get Local IP Address and Set in the IP Control
- How to get local machine name and IP address?
- local host ip get
- Get the image moments and the others related with it
- linux上获得本地IP地址和MAC地址(get local ip address and mac address)
- Basic way to get local IP
- C LANAGUE GET IP OF LOCAL MACHINE
- Global and Local Variables python
- python global and local variable
- bashrc get ip and deth
- gpmc and others
- APS AND OTHERS
- Eclipse and others
- .NET Permissions and others
- SIGINT and others
- MDX - TOPN AND OTHERS
- Python 8 Others
- GPS时间同步服务器
- shell中单引号、双引号、反引号的区别
- Linux下利用core文件跟踪错误
- 四种常用FPGA/CPLD设计思想与技巧
- 推荐网页制作规范
- python get local ip and sth. others
- USACO:PROB Your Ride Is Here
- [基础知识] raid5与raid10内部分析
- 关于C#命名空间的说明与用法
- jquery 插件 validate 的实例
- 查看Ubuntu版本
- RAID基础,RAID10与RAID01比较,RAID10与RAID5比较
- openfire外部组件的编写-这玩意干啥用的?
- flashdevelop生成swc库