Python query IP address attribution complete code

  • 2020-06-03 07:19:40
  • OfStack

This article shares the specific code of Python query IP address attribution for your reference, the specific content is as follows


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# To find the IP Place of Address 
#writer by keery_log
#Create time:2013-10-30
#Last update:2013-10-30
# usage : python chk_ip.py www.google.com |python chk_ip.py 8.8.8.8 |python chk_ip.py ip.txt
 
import signal
import urllib
import json
import sys,os,re
import socket
 
if len(sys.argv) <= 1 :
  print "Please input ip address !"
  sys.exit(0)
 
def handler(signum, frame):
  sys.exit(0)
signal.signal(signal.SIGINT, handler)
 
url = "http://ip.taobao.com/service/getIpInfo.php?ip="
 
# To find the IP address 
def ip_location(ip):
  data = urllib.urlopen(url + ip).read()
  datadict=json.loads(data)
 
  for oneinfo in datadict:
    if "code" == oneinfo:
      if datadict[oneinfo] == 0:
        return datadict["data"]["country"] + datadict["data"]["region"] + datadict["data"]["city"] + datadict["data"]["isp"]
 
# define IP And domain name regularization 
re_ipaddress = re.compile(r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
re_domain = re.compile(r'[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?')
 
if os.path.isfile(sys.argv[1]): # If the parameter is a file , Iterative search 
  file_path = sys.argv[1]
  fh = open(file_path,'r')
  for line in fh.readlines():
    if re_ipaddress.match(line):
      city_address = ip_location(line)
      print line.strip() + ":" + city_address
else:
  ip_address = sys.argv[1]
  if re_ipaddress.match(ip_address): # If the argument is single IP address 
    city_address = ip_location(ip_address)
    print ip_address + ":" + city_address
  elif(re_domain.match(ip_address)): # If the parameter is a domain name 
    result = socket.getaddrinfo(ip_address, None)
    ip_address = result[0][4][0]
    city_address = ip_location(ip_address)
    print ip_address.strip() + ":" + city_address

Related articles: