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. luogu P2900 [USACO08MAR]土地征用Land Acquisition

    写这道题时,预处理部分少打了等号,吓得我以为斜率优化错了或者被卡精了 mmp 首先有一个很明显的结论(逃),就是一个土地如果长(\(x\))与宽(\(y\))都比另一个土地小,那么这个土地一定可以跟那 ...

  2. dubbo框架原理

    Dubbo提供了三个关键功能:基于接口的远程调用,容错与负载均衡,服务自动注册与发现. Dubbo使得调用远程服务就像调用本地java服务一样简单. https://www.jianshu.com/p ...

  3. JavaScript学习 - 基础(七) - DOM event(事件)

    DOM event(事件) 定义事件: // 定义事件: //方式一,直接在标签上定义事件 // 方式二: var a11 = document.getElementsByName('a11')[0] ...

  4. cdh部署

    supermicro安装环境 本次安装基于无因特网的环境,共安装2个节点(一个master节点,一个data及节点),所用系统为centos7.x,所有安装过程均使用root用户.具体的节点信息如下: ...

  5. LOJ 3093: 洛谷 P5323: 「BJOI2019」光线

    题目传送门:LOJ #3093. 题意简述: 有 \(n\) 面玻璃,第 \(i\) 面的透光率为 \(a\),反射率为 \(b\). 问把这 \(n\) 面玻璃按顺序叠在一起后,\(n\) 层玻璃的 ...

  6. SpringBoot整合SpringDataElasticSearch操作ES

    (1).添加starter依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  7. Django配置图片上传

    本文首先实现django中上传图片的过程,然后解决富文本编辑器文件上传的问题. 一. 上传图片 1.在 settings.py 中配置MEDIA_URL  和 MEDIA_ROOT 在 D:\blog ...

  8. linux暂停一个在运行中的进程【转】

    转自:https://blog.csdn.net/Tim_phper/article/details/53536621 转载于: http://www.cszhi.com/20120328/linux ...

  9. 蓝牙Bluetooth技术手册规范下载【转】

    蓝牙Bluetooth技术手册规范下载 http://www.crifan.com/summary_bluetooth_specification_download/ [背景] 之前就已经整理和转帖了 ...

  10. ES系列十一、ES的index、store、_source、copy_to和all的区别

    1.基本概念 1.1._source 存储的原始数据._source中的内容就是搜索api返回的内容,如: { "query":{  "term":{   &q ...