IP Address Geolocation with Python (ipify.org, IP-API.com)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I hope you enjoy the following class do realize all of these video classes are based on what I teach at silicon Dojo here in Asheville North Carolina these are free to the end user Hands-On technology classes where basically you just sign up you show up and you get to learn technology I will remind you that free to the end user is not actually free all of this stuff cost me a shocking amount of money so if you would like to support the educational programs that I am creating the education that I deliver please think about clicking on the donor box link down below and throw in a couple of dollars in for the cause I hope you enjoy this class and more importantly I hope to see you at silicon Dojo sometime in the future welcome back as you know I am Eli the computer guy and in today's class we are going to go over geolocation using your external IP address so knowing the geographic location of a device can be a very valuable thing when you're trying to create a product products in the iot world the Internet of Things world right so at silicon dojo we do a lot of iot stuff with computer vision and computer voice actually being able to talk to computers instead of using keyboards those types of things and one of the interesting things to play around with is basically apis that provide information about things within your particular region so imagine a weather API so there's actually something called the open Weather API so you can ask what is the current weather the current humidity uh in a particular area you can ask for a forecast for a particular area there are news apis so for local news uh if you if you know your area you plug your your area in basically the API can spit out all of the local news for your area uh different things there's there's movie theaters right if you want to go out and go out and want to watch a movie and you want to see what movies are currently playing there are apis to show you what movies are currently playing one of the issues that you get into though in the iot world is most the time with iot you're not interacting with computers the way that we're used to interacting with computers again with keyboards and mice and even monitors right A lot of times we're actually talking to our computers and so our computers or our devices need to have an understanding of what their environment is in order to give you the best answer as easily as possible right so if having your computer understand where it is quite literally physically within the world that can be a useful thing to basically pass off as a value to an API call to say I need to know the weather in Asheville North Carolina I need to know the movie times in Atlanta Georgia that type of thing right so in order to get this we need to have geolocation now a lot of times when we think about geolocation we think about the the GPS unit that are built into our smartphones right so there's something called the HTML5 API that essentially uses JavaScript in order to communicate with the GPS on your smartphone and give a very specific lat long coordinates that can be sent off to a server in order to provide you the results that you need right that's what a lot of people think about and the problem is again with iot devices you don't necessarily have a GPS unit installed onto your particular device you may not want a GPS unit installed into your particular device and so the question then becomes how can you very easily tell this device more or less horseshoes hand grenades or nuclear weapons basically where in the world is this thing well that's where your external IP address comes in right so in the modern world of networking there's something called Network address translation all all your devices almost all your computer devices almost as in 99.9999 of them are going to be using something called CPI P4 this is an Internet Protocol they use something called IP addresses there's something called the internal IP address scheme so inside your network your Lan you'll have an IP address scheme of like 192.168.1.x so your server might be 192.168.1.10 your router might be 192.168.1.1 and this is a scheme that's replicated all over the world right there's probably probably like 20 of you going oh how did Eli know what my IP address is because because that's what we do again the 192 or the 10.0 those are what are called the internal IP address games they're actually non-internet routable so even if the IP address supposedly leaks onto the internet uh the internet's just not going to care it's going to ignore it right so that's where we have a Nat Network address translation so we have the internet IP addresses your Wan IP addresses the external IP addresses and so all of the routers that are connected to the internet any device that's drag Direct directly connected to the internet have that external IP address and essentially what happens when that Network address translation is your local internet your private internet address is translated through Network address translation to use your external IP address to go out to whatever web server that you're going to go to right but that external IP address one of the interesting things about it is it is very well documented right because those actually cost money right those external IP addresses one of the big things in the modern world is they cost money there's only like four 4.4 billion of those it is kind of a surprise that we're still using PC for app before again I literally I have a tcpi P4 video from 2009 where I talk about my um my teacher back in 2000 or 1999 somewhere around there uh said that we would have to learn tcpi before but don't worry about it it was about to become Obsolete and uh you know we would be using tcpv6 in no time at all uh yeah we're still using ipv4 2023 I'm recording this video uh we're still using ipv4 but anyways uh so all those external IP addresses are very well documented there there's actually blocks they are owned by isps or very large organizations and they document essentially more or less how all of those IP addresses are allocated right where where you know do those i p addresses connect to uh so if it's a large organization they may document that it connects to a specific building something like that for us most of the time for for Consumer internet access basically uh the the IP address blocks is documented what pool uh their ad all right so um so you'll you'll have with your router most likely you'll have what's called a dynamic IP address a dynamic Wan address that external IP address so every once in a while if you reboot your router if you do something like that it'll pull a new IP address basically it's like DHCP just like you have DHCP on the Lan there's more or less Horseshoes and hair grenades there is DHCP out for the isps and so basically what the isps do is they say okay well this IP address within this block we are going to document that all of that is for this particular area right uh so generally that you'll have your central office or if you have a cable internet they'll have something else like that but they'll have like some kind of central facility essentially they'll say all of these IEP addresses are allocated to the central facility and guess what and that's Central facility has an address it has a Latitude it has a longitude for coordinates it has a city that is in it has a state that is in as a country that is in uh has time time zones and all that kind of stuff and that's all literally just dumped into a database somewhere so you can actually use a service you can use a service to query where is this IP address right so basically you get your external IP address and you say where is this IP address you go to one of these database services and they say that IP address is again somewhere in Asheville North Carolina and again Horseshoes and Hand Grenades it will get you to the city it'll get you relatively close to where your physical device is so that you can then dump that information again into something like openweather.com or something like that so you can get that local information for you so in today's class basically what I'm going to do is I'm going to show you how to use these particular services so the first thing we're going to need to do is we're actually going to need to get our external IP address so if you know what IP address you want to query so you could use you could use this for people coming to your website so let's say you have a website people are coming to your website you just grab their IP addresses they they do not have to can in the sense they do not have to say yes or anything like that they come to your website it is a normal trans absolutely normal transaction it's part of how TCP ip4 works that the requesting a computer has to provide the IP address to the server computer and so the server computer can just collect that I that can take that IP address and then again do with it as it likes put that into a database use that IP address to query basically a geo geo um location database to figure out where that person is that type of thing but anyways so today we're going to say we don't have these people's IP addresses so that what we're going to do is we're first going to figure out what is our external IP address so we have this device again whether it's a computer whether it's a server some iot device your version of Siri whatever else we have this device it is connected to the internet first thing is how do we know what that external IP address is again not a 10.0 IP address not a 192.68. IP address whatever that extra Journal IP addresses then once we're able to get that external IP address we're going to plug that in to an API call to say where is this external IP address we're then going to get what's called a Json response so this is a data format response basically like a list or a uh an array if you're dealing with PHP world or something like that and then we can call particular values out of that Json response and then once we have those values again we can feed that into a different another API call or into a security procedure or something like that so with that let's go over the computer and I can show you how this works because this is really really really cool really useful and the awesome thing is I love what I love about the modern world is all API calls that are basically for free free you can just Hammer the hell out of these surfaces for free that's amazing okay so here we are at my computer so we have this open up in vs code this is python so again we have import requests up here all basically normal python code I am running this on my M2 Mac normally whenever we do these uh projects I'm using our lab computers the Ubuntu on those old MacBook Pros the reason that I'm actually using my M2 Mac today is because all you folks want to try to hack me or whatever and so essentially I'm using my little TunnelBear service here uh in order to use VPN so now I'm in Argentina or I can be in Australia or I can be in Austria so again one thing to think about with that external IP address is so that address is where you actually hit the internet from right so if you go through a VPN service then the external IP address that you're going to be receiving was will be the external IP address for that VPN provider right so if I was not connected to a VPN right now um and I went through this project today you would I don't know what you would do with it but you would do something stupid with the information that you found about my ISP or something like that so basically with this routing it through the VPN so as far as these services are concerned my external IP address is from the VPN itself right so here uh basically we're importing the request module and then here what we're doing is we're going to say I P address equals requests dot get and then HTTP API Dot ipfi.org and then we put dot text so basically what this is going to do is is going to get an object from this API this is it was called a rest API and then we're going to turn it into text and then we are going to print it out if I come up here and I just do command and I click on AP on this what we'll see is if I go to API ipfi.org all I'm receiving here all literally all I'm receiving back is simply the IP address my external IP address that is it this is a really cool service if you go to ipfi.org you can take a look at this a simple public IP address API get what you want basically you can Hammer the hell out of this you can use it without limit even if you're doing millions of requests per minute it's an open source project uh they say it's not logged they state it is not logged do I believe them of course not because I'm not stupid um anyways but it is it's very useful very fast and we come down here you can see the API usage so again if you guys want text which is what I would recommend is just get the text this is how you just get the text API API dot ipfi.org spits out text for you if you want Json so you can do the API ipfi.org format equals Json this will give you the Json So based basically you get the Json and then you have to reference the IP address by referencing IP I guess there's a reason for doing that I'm not doing it that way basically I'm just getting the text here and when I do that if I then do run so do the requests we do run and we can see down here I get 103.50.33.210 right so this is showing me what my external IP address is currently using this again as I said with that external IP address if we go here and I change let's say I want to be in Australia hopefully this doesn't fail on me okay so I'm gonna say that I'm in Australia now and I'm going to hit run on this and now it gives me an IP address of 45.76.118.254 again so this is that external IP address out there um again so uh we can track where we supposedly are in the world then we're going to come down here all right and we're going to do the requests again right so we're going to create a variable called response so response equals rip request dot get and we're doing an F string here right so what an F string does is it allows you to dump a a variable value inside of it and so we're making another API calling another rest API call so whenever you see these URLs these hyperlinks These are generally what are called rest apis HTTP IP hyphen api.com Json IP address so basically what this is going to do is it's going to give me the info information about this particular IP address if I go over and I take a look at their site again another a really cool service here let me refresh so you can see the information coming in from Australia so with this I can come down here and basically this is the kind of information that it can give you right so that 45.76.118.254 that's the external IP address the continent is Oceana the country is Australia country code is a uh city is Sydney zip code is 2015 lat latitude longitude time zone all this type of information is what you can get uh from this API what I like about this API is basically there is no there's no API key for this so I was using other IP address geolocation apis out there and honestly I have to say their API Key Systems sucked they were so bad so bad so you notice right if you watch my videos if you if you if you take my classes I've been doing a lot of stuff lately with uh open AI chai GPT Dolly those types of things um and basically their their API key system is really nice I create an API key I put into a project once I do the video I delete the API key and you're fine one of the interesting things that I found with some of this geolocation apis is oh my golly uh some of them you cannot change the API key you you create your API key and that is it you get that one API key so if that leaks to the world um you know is out there uh and there there's just a lot of tedious crap out there so one of the nice things with this is one it's it's free so it's free for non-commercial use for up to 45 requests per minute right so now again in the real world in a production environment 45 requests per minute might not be enough for you for an entire silicon dojo class if I have 10 people in a silicon Dozer class yes 45 requests per minute is fine and so that's that's not a there's no there's no daily cap there's no weekly cap there's no monthly cap it's just 45 requests per minute so free 45 requests per minute uh and it's pretty easy to do uh so let's go over here uh so basically I uh uncommented out this code so the response has gone equal request.get we're going to go to a IP hyphen api.com Json the IP address and then we're doing the dot Json at the end of this so basically it's going to make sure that the value that comes back will be in a Json format for python so that I'm able to basically go to specific specific indexes to pull out specific information so with this let's see if I'll uncomment this out just to show you what the response will be I just want to click on the go button so I get the IP address and then this is what the API response is in Json right so we get a status of success country is Australia country code is a u region and SW you know city is Sydney the whole nine yards so again basically you're getting a nice little Json response there the cool thing is again since we brought this in as Json here if I uncomment out that particular print we can print out the particular value that we want so let's say all we care about is the city value so I can print response City I can run this again so you get the IP address here you get the uh the Json response here and I get the city here and so this can be very valuable again for those uh oh for the apis that you're going to be using like weather apis that type of thing where you need a latitude longitude I can pull out the latitude or the longitude if I need let's say the city I can pull out the city if I need an approximate zip code so this can be a very valuable thing again not necessarily pulling your own external IP address but let's say if you have visitors coming to your website you want to get an idea of where the visitors are coming from being able to get those that kind of zip code can be a very useful thing I've talked about this you know previously for things like uh considering where you're going to expand your business let's say you have a pizza pizza delivery business and you're getting a lot of and you want to grow your business you want to create a second location you're getting a lot of calls from people that you cannot deliver to they're out of your delivery area basically if your website is collecting this information you could figure out which approximate zip codes have the most people that are coming to your website that where you're not able to help them and so that might be the next place that you want to expand to right you get all this kind of really nice information out here which is really pretty cool beyond that one of the interesting things with this particular API is you can actually ask for a lot more information so we go over to the API documentation here let's see here we can do the Json response formats Json uh one of the cool things here is there's actually this uh this little check off checkbox list and with this you can you can decide what information you want out of the Json response do you want status do you want message do you want continent or content code or country or country code or region or region name City District zip code lat long time zone Offset you want their currency do you know what currency they're in so I can check off this I want their currency uh let's see I want to know if they're on mobile I want to know if it's proxy I want to know if it's hosting right and so basically with that they give you this this nice big long uh query here I can then copy this I can go over here and basically dump dump that in there and then where am I looking for the IP address the IP address is there so basically I do IP address there so it'll now have the IP address and so with this I get the the status the message the country and again looking for something like the proxy are you interested if people are using proxy servers or VPN servers to get to your website do you have information on your website or your web app that's restricted to people that are only within a specific geographic location so I only want people that are in Asheville or let's say North Carolina I only want visitors who are in North Carolina well somebody starts using vpns so as far as their external IP address is concerned there they appear to be in North Carolina because that's where the IP address is located but I say no no no no no no I don't care what the IP address is located where the user is located so if they're using a VPN to be able to try to get access I want to cancel that right so if I click on this here basically what we'll find is near the end we can see proxy right so proxy so again equivalent as a VPN is true I am using a VPN in order to have this Australian IP address and so again depending on your particular situation you may care about that and shut them down or whatever else so anyways uh this is basically all there is to the geo location API and also how to get your external IP address a really nice easy to use you don't need any API Keys these are basically free especially on this this low low utilization so you should definitely definitely play with this stuff where in the world is my smart device anyways I'm a horrible singer do you guys do you guys still watch that do the kids still where in the World is Carmen San Diego I don't know I don't know if people still watch that anymore but now but now you will know more or less where in the world your particular iot device is and I think this is a really interesting thing to be thinking about again especially with the modern world of computers and Technology one of the things that I find very bizarre is i t people nowadays um computer people technology people many times we are still looking at computers and interacting and designing computer systems based off of how things were built literally 20 years ago again really think think about it we have series and we have Alexis and we have you know smart thermostats and that type of thing but how many people are really thinking about computers that way and really thinking about what does a computer need in order to understand its environment in order to be able to provide better responses to to the end user right so much of what we see anymore again if you go out and you talk with coders nowadays node and react and mobile apps and all that kind of thing and the funny part I gotta say I gotta say the funny part about mobile apps is really mobile apps look a hell of a lot like the apps we've had for 40 years at this point they're just you know definitely got a faster refresh and all that and you can you can touch the screen and that type of thing but there's a lot of this stuff a lot of this design a lot of folks aren't thinking about is again in this modern world with technology how can we start how can we start having these systems better understand their environments and building again for for normal people to build systems like you you right now can sit there and grab a crappy 2012 MacBook Pro and literally build a computer device that you can talk to that it will respond to you with speech that you can write codes it'll tell you the time it'll tell you you the weather it'll tell you movies and you know movie schedules and all that kind of thing and you don't you don't have to go through Siri you don't have to go through Alexa you don't have to go through whatever the hell it is that Google has you can actually build all this with relatively simple apis um and uh again I think it's one of the the weird things that I don't see more people kind of focused on because I I think this stuff is really awesome but anyways uh with this with the whole geo location uh the important thing to understand again you're not getting a precise you're not getting precise right so when you have a smartphone and you get that lat long coordinate from the HTML5 API that is per se that is GPS precise however precise GPSs are at this point in time that's how precise that is again it is important to understand with this it is not that precise but again it gets you to the city uh the other thing to be thinking about uh with these types of systems is that they are database type systems as in there's a database somewhere that holds this information and so when you make the query you're making a query to this vendor's database why that's important so the service that I just showed you is free 45 requests per minute and it's good enough for all you folks good enough for me it's good enough for my students it's probably good enough for most of you one of the things to look at though is with this type of service there are other vendors out there that actually costs a surprising amount of money it can be like a hundred dollars for 150 000 requests which again for something like a website because it does get to be pretty expensive but one of the things that they they said they they sell is uh basically that they update their databases faster than other vendors update their databases right so again with these IP addresses uh you know that whatever the idea 45.76.118.254 uh this is a this is a type of digital property right somebody actually has ownership of this IP address or really more these blocks of IP addresses so these blocks of IP addresses they can get moved within a company right they could decide oh we have more people we have more new customers over in Atlanta or something so we're going to take this block and move it to Atlanta instead of being in Asheville so that might be something that wasn't properly updated in the system entire blocks of IP addresses uh could be could be sold so you may hear of an ISP going out of business or going bankrupt something like that they may sell blocks of IP addresses but when they sell the block IP address they can you know you could have Comcast selling to Spectrum Spectrum goes off and uses the blocks for for whatever they're going to use right the important thing is though it's not like the database auto updates somebody again or always remembered somebody's got to auto update the database nothing in the technology world is Autumn is actually magical right somebody's got to do it something has got to do it so if you're using one vendor that may be using outdated uh geolocation information for IP addresses you might start getting some funky results and so that that's why because a lot of people are like well if this is free or more or less free why the hell would I spend 100 or whatever for 150 000 requests per month and the reason is is because that actually might be much more up to date than what these folks you know have and again it's one of those things that you have to be thinking about again as I talk about that in the technology world so many people think technology professionals uh it's all about again the API calls subnet masking that type of thing again real professionals and Professionals in the tech world get paid real money it's thinking about things like this it's thinking about okay if I go with his vendor how how often are the tables updated for this particular vendor versus a different vendor so if I have to spend 50 more dollars a month for this vendor but I get better results that might actually make a lot more sense right this is the kind of thing that you really have to think about so anyways that's about all I got to say that's a very that is a very very long-winded class when something that I could probably explain in about 35 seconds but anyways you didn't you didn't come here you didn't come here for a 35 second video if you did come here for a 35 second video well you've stuck around you've stuck around for whatever 30 minutes or however long it's been so anyways uh as always I enjoyed teaching this class and look forward to seeing at the next one
Info
Channel: Eli the Computer Guy
Views: 4,524
Rating: undefined out of 5
Keywords: Eli, the, Computer, Guy, Repair, Networking, Tech, IT, Startup, Arduino, iot
Id: yZmPipD2KdM
Channel Id: undefined
Length: 29min 28sec (1768 seconds)
Published: Tue Sep 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.