我想制作一个只有IP格式的简单Python IP Tracker,但我很困惑因为我无法过滤输入。 这是我的代码:
while True:
ip= raw_input("What Your Target IP : ")
url = "http://blabla.com/json/"
response = urllib2.urlopen(url + ip)
data = response.read()
values = json.loads(data)
print("------------------------------------")
print "\r"
print(" IP: " + values['query'])
print(" City: " + values['city'])
print(" Region: " + values['regionName'])
print(" Country: " + values['country'])
print(" Time Zone: " + values['timezone'])
print "\r"
break
答案 0 :(得分:0)
您可以使用正则表达式或IP地址模块来解决您的问题
import re
import os
import urllib2
import json
while True:
ip = raw_input("What Your Target IP : ")
if not re.match(("^\d{0,9}\.\d{0,9}\.\d{0,9}\.\d{0,9}$"), ip):
print ("Error! Make sure you only use numbers")
else:
print("You picked number "+ ip)
url = "https://blablabla/"
response = urllib2.urlopen(url + ip)
data = response.read()
values = json.loads(data)
print("------------------------------------")
print "\r"
print(" IP : " + values['ip'])
print(" City : " + values['city'])
print(" Region : " + values['region'])
print(" Country : " + values['country_name'])
print(" Continent : " + values['continent_name'])
print(" Time Zone : " + values['time_zone'])
print(" Currency : " + values['currency'])
print(" Calling Code : " + "+" + values['calling_code'])
print(" Organisation : " + values['organisation'])
print(" ASN : " + values['asn'])
print "\r"