萌新第一次发帖,求老司机关照
上代码
[Python] 纯文本查看 复制代码 #!/usr/bin/python
# coding:utf-8
import sys, requests
import json
def get_addr(ip):
api_url = "http://ip.taobao.com/service/getIpInfo.php"
payload = {'ip': ip}
r = requests.get(api_url, params=payload)
info = r.json()
ret_code = info.get("code")
if ret_code == 0:
ret_data = info.get("data")
isp = ret_data.get("isp")
city = ret_data.get("city")
country = ret_data.get("country")
print("--- " + ip + " ---")
print("Country: " + country)
print("City: " + city)
print("ISP: " + isp)
else:
print("查询失败!")
def usage():
print("Usage: " + sys.argv[0] + " 8.8.8.8")
if __name__ == '__main__':
try:
ip = sys.argv[1]
get_addr(ip)
except:
usage()
|