python获取公网ip的几种方式

 
 
  1.  
    from urllib2 import urlopen
  2.  
    my_ip = urlopen('http://ip.42.pl/raw').read()
  3.  
    print 'ip.42.pl', my_ip
  4.  
     
  5.  
    from json import load
  6.  
    from urllib2 import urlopen
  7.  
     
  8.  
    my_ip = load(urlopen('http://jsonip.com'))['ip']
  9.  
    print 'jsonip.com', my_ip
  10.  
     
  11.  
    from json import load
  12.  
    from urllib2 import urlopen
  13.  
     
  14.  
    my_ip = load(urlopen('http://httpbin.org/ip'))['origin']
  15.  
    print 'httpbin.org', my_ip
  16.  
     
  17.  
    from json import load
  18.  
    from urllib2 import urlopen
  19.  
     
  20.  
    my_ip = load(urlopen('https://api.ipify.org/?format=json'))['ip']
  21.  
    print 'api.ipify.org', my_ip

python 获取ip信息(国家、城市等)

 https://blog.csdn.net/qf0129/article/details/83145765

python 获取ip信息(国家、城市等)

安装geoip2包:pip install geoip2
下载数据库文件:http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz
解压出GeoLite2-City.mmdb文件
新建py文件,内容如下:

import geoip2.database

reader = geoip2.database.Reader('./GeoLite2-City.mmdb')  # mmdb文件路径
c= reader.country('123.123.123.123')
print c.country.names # 国家名
print c.country.code # 国家代码
# 其他信息看源码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

** 以下为配置自动更新数据库的步骤 **

在github下载geoipupdate最新包:geoipupdate-3.1.1.tar.gz
解压并安装:

tar -zxvf geoipupdate-3.1.1.tar.gz
./configure
sudo make
sudo make install
  • 1
  • 2
  • 3
  • 4

修改配置文件:/usr/local/etc/GeoIP.conf

# The following AccountID and LicenseKey are required placeholders.
# For geoipupdate versions earlier than 2.5.0, use UserId here instead of AccountID.
AccountID 0
LicenseKey 000000000000 # Include one or more of the following edition IDs:
# * GeoLite2-City - GeoLite 2 City
# * GeoLite2-Country - GeoLite2 Country
# For geoipupdate versions earlier than 2.5.0, use ProductIds here instead of EditionIDs.
EditionIDs GeoLite2-City GeoLite2-Country
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

执行更新:
/usr/local/bin/geoipupdate

添加crontab实现每周更新:crontab -e
0 0 * * 0 /usr/local/bin/geoipupdate

 
 

python 获取本机IP的三种方式

 

python获取本机IP的方式

第一种:

#!/usr/bin/python 

import socket
import fcntl
import struct
def get_ip_address(ifname):
  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
  s.fileno(),
  0x8915, # SIOCGIFADDR
  struct.pack('256s', ifname[:15])
)[20:24])
#get_ip_address('lo')环回地址
#get_ip_address('eth0')主机ip地址

第二种:

def get_local_ip(ifname):
  import socket, fcntl, struct
  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))
  ret = socket.inet_ntoa(inet[20:24])
  return ret
print(get_local_ip("eth0"))

第三种:

import socket
print(socket.gethostbyname(socket.getfqdn(socket.gethostname())))

python获取公网ip,本地ip及所在国家城市等相关信息收藏的更多相关文章

  1. python获取公网ip的几种方式

    python获取公网ip的几种方式 转 https://blog.csdn.net/conquerwave/article/details/77666226 from urllib2 import u ...

  2. python 获取本机的IP

    python 获取本地的IP import socket import fcntl import struct def get_ip_address(ifname): s = socket.socke ...

  3. 利用python获取nginx服务的ip以及流量统计信息

    #!/usr/bin/python #coding=utf8 log_file = "/usr/local/nginx/logs/access.log" with open(log ...

  4. 多媒体开发之--- Live555 server 获取不到本地ip 全为0

    今天把wis-streamer live555 移植到8148上面跑起来了,运行testOnDemandRTSPServer的时候发现,本地IP地址居然为0.0.0.0; 于是乎就跟踪调试了下,看看它 ...

  5. 使用Python获取计算机名,ip地址,mac地址等等

    获取计算机名 # 获取计算机名,常用的方法有三种 import os import socket # method one name = socket.gethostname() print(name ...

  6. python获取本机的IP

    转载:https://www.cnblogs.com/whu-2017/p/8986842.html 方法一: 通常使用socket.gethostbyname()方法即可获取本机IP地址,但有时候获 ...

  7. 电脑ip(本地ip和本机ip)

    1.localhost:localhost 是个域名,不是地址,它可以被配置为任意的 IP 地址,不过通常情况下都指向 127.0.0.1(ipv4)和 [::1](ipv6) 2.127.0.0.1 ...

  8. python 获取公网 ip

    from urllib2 import urlopen my_ip = urlopen('http://ip.42.pl/raw').read() print 'ip.42.pl', my_ip fr ...

  9. python获取linux本机IP

    #!/usr/bin/env python #encoding: utf-8 #description: get local ip address import os import socket, f ...

随机推荐

  1. 第5月第6天 NSOperation isConcurrent category同名覆盖

    1. @implementation AFURLConnectionOperation ... - (BOOL)isConcurrent { return YES; } NSOperation调用st ...

  2. Sqoop异常:Please set $ACCUMULO_HOME to the root of your Accumulo installation.

    shell-init: 获取当前目录时出错: getcwd: 无法访问父目录: 没有那个文件或目录chdir: 获取当前目录时出错: getcwd: 无法访问父目录: 没有那个文件或目录chdir: ...

  3. Javascript定义变量

    在JavaScript中通过var来定义变量,不管是数字还是字符串,都可以通过这种方式来定义:我们既可以在声明变量的同时给变量赋值,也可以先声明变量,再给变量赋值. <script> va ...

  4. HTML5实现全屏API【进入和退出全屏】

    现在主流浏览器基本上实现了全屏效果,但是不同浏览器实现不一样: [进入和退出全屏] // Webkit (works in Safari5.1 and Chrome 15)element.webkit ...

  5. spring-framework-x.x.x.RELEASE-dist下载教程

    1.打开Spring官网:https://spring.io,点击PROJECTS 2.点击SPRING FRAMEWORK 3.点击GitHub图标 4.找到Access to Binaries,点 ...

  6. 【tomcat】sessionId学习(未完待续)

    这里主要研究tomcat中session的管理方式以及sessionId的原理,下文将研究sessionid存到redis中以及基于redis实现session共享. 平时也就是了解session是基 ...

  7. Dubbo高可用

    高可用:通过设计减少系统不能提供服务的时间 (1).zookeeper宕机 原因:zookeeper宕机 现象:zookeeper注册中心宕机,还可以消费dubbo暴露的服务. 健壮性: 监控中心宕掉 ...

  8. JavaScript对象复制(二)

    <script> function copy(a) { ret = {}; for (sth in a) { temp = a[sth]; if (temp instanceof Arra ...

  9. 解决vmware虚拟机克隆后启动centos报错device eth0 does not seem to be present, delaying initialization

    centos启动报错: device eth0 does not seem to be present, delaying initialization ifcfg-eth0的配置文件里保存了以前的M ...

  10. 有关cookie

    cookie     会话跟踪技术               <script>             /*                 cookie 全称  会话跟踪技术.     ...