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. Error while executing topic command : Replication factor: 2 larger than available brokers: 0.

    [root@hdp1 /mnt/software/maxwell-1.19.4]#kafka-topics.sh --zookeeper hdp1,hdp2,hdp3:2181 --create -- ...

  2. pygame中多个class类之间的关系

    用一个实例介绍一下有关pygame中不同类之间的通信,详细介绍在代码段有标注,感兴趣的可以复制代码试试: import pygame import sys # -------------------- ...

  3. CEdit使用(Edit Control控件)

    CEdit使用(Edit Control控件) 编辑框只读 属性 Read Only等于 True,就有了只读效果 密码框 属性 Password 等于 True,就有了密码效果 获取值/赋予值 Up ...

  4. 使用matplotlib绘制多个图形单独显示

    使用matplotlib绘制多个图形单独显示 一 代码 import numpy as np import matplotlib.pyplot as plt #创建自变量数组 x= np.linspa ...

  5. canny 算子python实现

    1. Canny介绍 Canny算子与Marr(LoG)边缘检测方法类似,也属于是先平滑后求导数的方法.John Canny研究了最优边缘检测方法所需的特性,给出了评价边缘检测性能优劣的三个指标: 1 ...

  6. OLAP和OLTP的区别(基础知识) 【转】

    联机分析处理 (OLAP) 的概念最早是由关系数据库之父E.F.Codd于1993年提出的,他同时提出了关于OLAP的12条准则.OLAP的提出引起了很大的反响,OLAP作为一类产品同联机事务处理 ( ...

  7. mac 报错Root chmod operation not permitted on file

    系统:mac os 10.14.1 重启电脑 mac用户在升级系统之后,电脑启用了SIP(System Integrity Protection),增加了rootless机制,导致即使在root权限下 ...

  8. vuejs初学入门环境搭建

    一.Nodejs:    1.安装下载:http://nodejs.cn/download/    2.Node.js安装配置: http://www.runoob.com/nodejs/nodejs ...

  9. [学习笔记]Javascript可选的分号

    1.前言 由于Javascript有自动识别一句语句的结尾,但是缺少必要分号作为结尾符,会降低代码的可读性和整洁性.通过javascript权威指南By淘宝前端团队译,这分号还算比较好玩的. 2.样例 ...

  10. STM32应用实例八:与多台MS5803压力传感器I2C通讯

    MS5803压力传感器支持SPI和I2C总线通讯,拥有24位AD转换.能够同时获得压力值和温度值,其中压力测量范围为10-1100mbar,温度的测量范围是-40-85摄氏度.各引脚功能及参数如下: ...