Skip to content

Instantly share code, notes, and snippets.

@suziewong suziewong/ip.py
Last active Mar 2, 2018

Embed
What would you like to do?
根据域名查IP,物理地址
#!//usr/bin/python
# encoding=utf-8
import sys
import socket
import urllib2
import json
import re
if __name__ =="__main__":
if len(sys.argv) == 2:
domain = sys.argv[1]
# 获取域名的IP地址
try:
domain = re.search('(http|https)://(.*)/',domain).group(2)
except:
print "Wrong domain OR IP"
sys.exit(2)
result = socket.getaddrinfo(domain, None)
ip = result[0][4][0]
url = "http://ip.taobao.com/service/getIpInfo.php?ip="+ip
res = urllib2.urlopen(url)
data = res.read()
objs = json.loads(data)
if re.findall(r'\d+.\d+.\d+.\d+', domain):
domain = ''
if objs['data']:
print domain,ip,objs['data']['country'],objs['data']['region'],objs['data']['city'],objs['data']['isp']
else:
print domain,ip
else:
print "Usage():"
print " ip.py www.taobao.com|http://www.taobao.com/|42.1.1.1"
sys.exit(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.