-2

I want to make a simple Python IP Tracker with IP Format only, but i'm confused cause i can't filter the input. This is my code:

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
Abay
  • 35
  • 1
  • 1
  • 8
  • 1
    Use the stdlib [ipaddress](https://docs.python.org/3/library/ipaddress.html#module-ipaddress) module. – ekhumoro Feb 25 '18 at 18:06
  • Your title is misleading. You don't want to check for numbers, you want to check for valid IP addresses. – avigil Feb 25 '18 at 19:08

1 Answers1

0

You can use regular expression or IP address module to resolve your problem

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"
Harsh Bhut
  • 238
  • 2
  • 14