Last active
December 29, 2015 17:09
-
-
Save mozillazg/7701657 to your computer and use it in GitHub Desktop.
get ip info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import json | |
import sys | |
import urllib | |
if __name__ == '__main__': | |
args = sys.argv | |
if len(args) < 2: | |
ip = 'myip' | |
else: | |
ip = args[1] | |
url = 'http://ip.taobao.com/service/getIpInfo.php?ip={0}'.format(ip) | |
response = urllib.urlopen(url) | |
body = json.loads(response.read()) | |
code = body['code'] | |
if code == 0: | |
data = body['data'] | |
for key, value in data.items(): | |
print(u'{0:<12}: {1}'.format(key, value)) | |
else: | |
print('Get ip info fail!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment