2017-03-15 18 views
0

IPアドレスを国コードに変換するPythonスクリプト。 情報はウェブサイト 'https://freegeoip.net/json/'から取得されます。国コードPythonでスクリプトを見つける

これ以外の方法はありますか?

import json 
import requests 
import subprocess 

# clearing the screen. 
subprocess.call('clear',shell=True) 
msg = 'country code finder' 

# Making the header. 
print('') 
print(' ',msg.upper()) 
print(' ','-' * len(msg)) 
print('') 

lookup_ip = input(' Enter Your Ip Address : ') 

# Fetching the country code from 'https://freegeoip.net/json/' 
# The reply will be in json format. 
try: 

    lookup_string = 'https://freegeoip.net/json/'+lookup_ip 
    reply = requests.get(lookup_string) 
    geodata= json.loads(reply.text) 
    print('') 
    print(' Country Is ::' , geodata['country_code']) 
    print('') 

except: 
    print(' Unable to Fetch', lookup_string) 
    print('') 
+0

質問がありますか? – Cfreak

+0

http://codereview.stackexchange.com/に属しているため、この質問を議論の対象外としています – kdopen

答えて

1

あります。

import requests 


ip = input('Enter Your Ip Address: ') 
try: 
    response = requests.get('https://freegeoip.net/json/{0}'.format(ip)) 
    response.raise_for_status() 
    json = response.json() 
    print('Country is {0}'.format(json.get('country_code', 'unknown'))) 
except: 
    print('Unable to fetch') 
関連する問題