Home  >  Article  >  Backend Development  >  python实现查询IP地址所在地

python实现查询IP地址所在地

WBOY
WBOYOriginal
2016-06-06 11:23:061676browse

使用IP138数据库查询域名或IP地址对应的地理位置。

#-*- coding:gbk -*-
import urllib2
import re
 
try:
  while True:
    ipaddr = raw_input("Enter IP Or Domain Name:")
    if ipaddr == "" or ipaddr == 'exit':
      break
    else:
      url = "http://www.ip138.com/ips138.asp?ip=%s&action=2" % ipaddr
      u = urllib2.urlopen(url)
      s = u.read()
      #Get IP Address
      ip = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',s)
      print "\n****** Below Result From IP138 Database *****"
      print "IP Address:",ip[0]
      #Get IP Address Location
      result = re.findall(r'(<li>.*&#63;</li>)',s)
      for i in result:
        print i[4:-5]
      print "*"*45
      print "\n"
 
except:
  print "Not Data Find"

以上所述就是本文的全部内容了希望大家能够喜欢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn