-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
2
  • 1
    Use the stdlib ipaddress module. Commented Feb 25, 2018 at 18:06
  • Your title is misleading. You don't want to check for numbers, you want to check for valid IP addresses. Commented Feb 25, 2018 at 19:08

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

10 Comments

I got an error here, but i don't know how to use code formatting in comment.
print ("Error! Make sure you only use numbers") ^ IndentationError: unindent does not match any outer indentation level
got an error in )
Check this link < it's my code after your answer
Your indentation does not match the answer suggested here
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.