IP Geolocation using Python with ip2geotools and geoip2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so in this video we will be using python to get uh geolocation um for a specific IP address so the first tool that we'll be using is called ip2 Geo tools um this one is very quick and easy to use very simple to implement and also has a free version the second one that we'll be using is called Geo ip2 uh this one also has a a free downloadable um database from maxmind uh that we'll be uh using okay so I have an empty folder set up here so the first thing we need to do is install the library however I'm using uh pip EnV if you're not familiar with Pip EnV there's a link in the description to how I have it set up okay so the package should be installed I go ahead and clear my terminal and there are a number of sub dependencies um so we only installed one package here and it just grabbed all these what these are these are different uh providers that you can use so there's Max mine there's ip2 location there's a few more also you notice that request is um also being installed so what happens is every time you call this API as we will uh soon demonstrate you're actually making an HTTP request under the hood so you want to keep that in mind because if you have too many at uh requests at once you might get throttled or it might just be um added latency so that's the one disadvantage of this okay so I'm going to go ahead and create a new file okay Switching to the editor here so what we need to do is import the library so we want ip2 geot tools. databases. noncommercial and we want the uh DB IP City class so I'm going to create a new variable here called IP location and we want to call the get method and the first um thing that we're going to pass in is the actual IP address and there's a second parameter um that that which is the API key since we're using the free version we'll just pass in free okay so once we get a result back there's a number of properties that um we have so I'm going to go ahead and just uh paste those in okay one thing to note here is that um this Library I'll actually return a latitude and longitude which the other one will not so if that's something that uh you need I mean it might save you having to do some uh Geo other geoc code lookup for that okay so I'm outside of the shell since I'm using pip andv have to go into inside the shell okay so now we have our result back um so we have uh country region City so it's someplace in Hawaii along with a latitude and longitude and that's really all you need to implement this um like I said there's a number of different providers so if you want to upgrade in the future um you can upgrade to one of the commercial ones um it's it's a paid service but it's going to give you more accurate information um and it's going to be more up to dat and another benefit of this whole approach is there's very minimal code changes that you would have to do to upgrade okay so for the goo ip2 implementation um what we want is uh this free geolocation data now there are two different um things that this can provide um so there's one it could be a paid web service or if you download the um max mine database file um you can just use it locally so this uh URL does change periodically um um so you could probably find it just by Googling goly to free location free geolocation data and before you used to be able to just go straight to the downloads page and uh download the file um now you do have to go through this um sign up process so if you click here um you have to fill out this form uh with your email and then what they do is they send you an email verification and then you'll be able to set your password um log in and then be able to access the uh database um so the download databases page um once you're logged in um what you want to do is uh scroll down you should see go2 light goite 2 City and then download this uh gzip file okay Switching back to our terminal here once you have that file downloaded um what I'm going to do in here is just uh create a new uh directory called DB and go ahead and CD into it so we want to do is copy uh the file we downloaded and let's go ahead and copy it to here so if I so if I look at the file um so what we have is this um zip file here so in order to unarchive this we just use the tar command and then we need passing some parameters so x uh V CF and then our file name so that extracted a few files so if I do a listing on this um so we have a this right here is a folder um so we'll just uh list those files out and the file that we're interested in is this goly 2- city. mmdb this is the actual database right here so I'm goe and create a new file call this Geo ip2 demo and so we need to import the uh database or the library okay so we're going to create a new variable called reader so we want G 2. database. Reader um the reader object so the first thing we need to do is pass in our passing our path to the uh database so we have this folder here and then um geo2 light city. mmdb I'm going to create a new variable called response so we want reader. City and then I'm going to use that same IP from the previous example for the um uh ip2 Geo tools okay and just like the previous example there's a number of properties that we will get back from our response and I'm just going to go ahead and paste those in and then after that we want to just close our reader okay so scrolling back up um what they so they do return us um actual standardized ISO codes um like us for example which is good and this is a little bit odd here um so in order to get like the state name you have to go response. subdivisions um most specific. name um to get the state code the state has its own ISO code um so we have to use the subdivisions um object for that also and then um response uh. City so City and um postal code those are a little bit more straightforward so I'm going to go ahead and save this file and then switch back to the terminal to run it okay and then there's our results um just as it was highlighted in the comments um it made look made that check inside the database uh a local file and then um printed out the result so this approach might be a better option for you if downloading the um database and especially if you have to make a ton of requests in a short amount of time this is probably the best way to go it's going to be the fastest and you don't have to worry about uh latency and throttling and other issues like that
Info
Channel: kishstats
Views: 2,003
Rating: undefined out of 5
Keywords:
Id: XSBqp9vzu4I
Channel Id: undefined
Length: 8min 35sec (515 seconds)
Published: Mon Oct 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.