Package: geoip2fast
GeoIP2Fast v1.2.1GeoIP2Fast is the fastest GeoIP2 country/city/asn lookup library. A search takes less than 0.00003 seconds. It has its own data file updated with Maxmind-Geolite2-CSV, supports IPv4 and IPv6, works on Python3, Pypy3 and is Pure Python!With it´s own datafile (geoip2fast.dat.gz), can be loaded into memory in ~0.07 seconds and has a small footprint for all data, so you don´t need to make requests to any webservices or connect to an external database. And it works on Linux, Windows and MacOS!And now you can download .dat.gz from your own application or via the command line. See more atUpdate your database automaticallyGeoIP2Fast returns ASN NAME, COUNTRY ISO CODE, COUNTRY NAME, CITY NAME/STATE/DISTRICT and CIDR. There is no external dependencies, you just need thegeoip2fast.pyfile and the desired data file.dat.gz. The lookup speed is the same for any data file.Country and ASN databases with IPv4 and IPv6Download ContentFile NameFile SizeLoad TimeRAM FootprintCountry IPv4geoip2fast.dat.gz1.2 MiB~0.04 sec~22.0 MiBCountry IPv4+IPv6geoip2fast-ipv6.dat.gz1.8 MiB~0.07 sec~41.0 MiBCountry+ASN IPv4geoip2fast-asn.dat.gz2.3 MiB~0.08 sec~40.0 MiBCountry+ASN IPv4+IPv6geoip2fast-asn-ipv6.dat.gz3.1 MiB~0.15 sec~97.0 MiBCity and ASN databases with IPv4 and IPv6City databases are not included in the pip installation package, if you want to:download them manually fromDAT Files LATEST Releaseor create them yourself usinggeoip2dat.py.City data already includes country data.or download using the new function GeoIP2Fast.update_all()read more belowDownload ContentFile NameFile SizeLoad TimeRAM FootprintCity IPv4geoip2fast-city.dat.gz12 MiB~0.50 sec~270.0 MiBCity IPv4+IPv6geoip2fast-city-ipv6.dat.gz14 MiB~0.60 sec~400.0 MiBCity+ASN IPv4geoip2fast-city-asn.dat.gz13 MiB~0.60 sec~310.0 MiBCity+ASN IPv4+IPv6geoip2fast-city-asn-ipv6.dat.gz17 MiB~0.70 sec~430.0 MiBIf you are using pypy3, the loading time will increase a little, but the speed will be a little faster and the memory footprint will be lower.The new v1.2.X databases are not compatible with versions equal to or older than v1.1.X, and vice versa, obviously. The last version with support only for countries and ASN only is v1.1.X, which is still available with this command line:pip install geoip2fast==1.1.10.What's new in v1.2.1 - 01/Dec/2023
- DAT files updated with MAXMIND:GeoLite2-CSV_20231201
- Improved speed! faster than ever!
- Automatic updates! you can update to the newest dat.gz file via command line or via code
  Using command line:
      # to update all files by saving them in the library directory
      geoip2fast --update-all -v

      # to update a single file replacing the default file 
      geoip2fast --update-file geoip2fast-city-asn-ipv6.dat.gz --dest geoip2fast.dat.gz -v

  Using the class GeoIP2Fast:
      from geoip2fast import GeoIP2Fast
      from pprint import pprint
      G = GeoIP2Fast(verbose=True)
      G.get_database_path()
      
      # to update a single file replacing the default file 
      update_file_result = G.update_file(filename="geoip2fast-asn-ipv6.dat.gz",destination="geoip2fast.dat.gz",verbose=True)
      pprint(update_file_result,sort_dicts=False)
      G.reload_data(verbose=True)

      # to update all files by saving them in the library directory
      update_all_result = G.update_all(verbose=True)
      pprint(update_all_result,sort_dicts=False)
      G.reload_data(verbose=True)

      # to update all files SILENTLY
      update_all_result = G.update_all(verbose=False)
      errors_result = [item for item in update_all_result if item['error'] is not None]
      if len(errors_result):
         print(f"There were {str(len(errors_result))} update failures")
      G.reload_data(verbose=False)

      # to update all files by saving them in the directory of your application
      update_all_result = G.update_all(destination_path=".",verbose=True)
      pprint(update_all_result,sort_dicts=False)
      G.reload_data(verbose=True)Installationpipinstallgeoip2fastDAT files updatesYou can create your own dat.gz file usinggeoip2dat.pyfile.You can alsodownload the latest dat filesthat are updated automatically on Tuesdays and FridaysAnd you canupdate the dat files downloading from our releases repository, via code or via command line.How does it work?GeoIP2Fast has 4 datafiles included. Tha main file isgeoip2fast.dat.gzwith support Country lookups and only IPv4. Usually, these files are located into the library directory (/usr/local/lib/python3.XX/dist-packages/geoip2fast), but you can place it into the same directory of your application. The library automatically checks both paths, And the directory of your application overlaps the directory of the library. You can use an specific location also.Thebisect()function is used together with some ordered lists of integers to search the Network/CountryCode (Yes! an IP address has an integer representation, try to ping this number:ping 134744072or thisping 2130706433).If GeoIP2Fast does not have a network IP address that was requested, a "not found in database" error will be returned. Unlike many other libraries that when not finding a requested network, gives you the geographical location of the network immediately below. The result is not always correct.Data file updates are made available twice a week in ourgithub releases. Files with City data and IPv6 support can also be downloaded there.There are network gaps in the files we use as a source of data, and these missing networks are probably addresses that those responsible have not yet declared their location. Of all IPv4 on the internet (almost 4.3 billions IPs), our coverage is around 99.64% and we do not have information on approximately 15 million of them (~0,36%). It must be remembered that the geographical accuracy is the responsibility of the network block owners. If the owner (aka ASN) of the XXX.YYY.ZZZ.D/24 network range declares that his network range is located at "Foo Island", we must believe that an IP address of that network is there.Don't go to Foo Island visit a girl you met on the internet just because you looked up her IP on GeoIP2Fast and the result indicated that she is there.Quick StartOnce the object is created, GeoIP2Fast loads automatically all needed data into memory. The lookup function returns an object calledGeoIPDetail. And you can get the values of it's properties just calling the name of proprerty:result.ip,result.country_code,result.country_name,result.city.name,result.city.subsubdivision_code,result.city.subsubdivision_name,result.cidr,result.is_private,result.asn_name,result.asn_cidrandresult.elapsed_time. Or use the functionto_dict()to get the result as a dict. You can get values likeresult.to_dict()['country_code']When using it as a Python library, if you want to load a file other thangeoip2fast.dat.gz, you simply create the object by typing the name of the file you want to work with. The library first looks for the given file in the current directory and then in the library directory. If desired, you can directly specify the path. If don´t specify any file, the defaultgeoip2fast.dat.gzwill be used. Example:>>>fromgeoip2fastimportGeoIP2Fast>>>geoip=GeoIP2Fast(geoip2fast_data_file="/my_data_files_path/geoip2fast-city-asn-ipv6.dat.gz",verbose=True)GeoIP2Fastv1.2.1isready!geoip2fast-city-asn-ipv6.dat.gzloadedwith4.488.721networksin0.54506secondsandusing426.28MiB.>>>print(geoip.lookup("191.251.128.1").pp_json()){"ip":"191.251.128.1","country_code":"BR","country_name":"Brazil","city":{"name":"Araraquara","subdivision_code":"SP","subdivision_name":"Sao Paulo"},"cidr":"191.251.128.0/20","hostname":"","asn_name":"TELEFONICA BRASIL S.A","asn_cidr":"191.250.0.0/15","is_private":false,"elapsed_time":"0.000147356 sec"}>>>geoip.get_database_path()'/my_data_files_path/geoip2fast-city-asn-ipv6.dat.gz'>>>>>>result=geoip.lookup("3.2.35.65")>>>result.city.name'São Paulo'>>>result.city.subdivision1_code'SP'>>>result.country_name'Brazil'>>>result.country_code'BR'>>>result.asn_name'AMAZON-02'>>>result.elapsed_time'0.000011612 sec'>>>>>>result=geoip.lookup("10.20.30.40")>>>result.country_name'Private Network Class A'>>>result.cidr'10.0.0.0/8'>>>result.is_privateTrue>>>fromgeoip2fastimportGeoIP2FastGEOIP=GeoIP2Fast()result=GEOIP.lookup("200.204.0.10")print(result)# to use the country_code propertyprint(result.country_code)# to print the ASN name propertyprint(result.asn_name)# Before call the function get_hostname(), the property hostname will always be empty.print("Hostname: "+result.hostname)result.get_hostname()print("Hostname: "+result.hostname)# to work with output as a dict, use the function to_dict()print(result.to_dict()['country_code'],result.to_dict()['country_name'])# to check the date of the CSV files used to create the .dat fileprint(GEOIP.get_source_info())# to show the path of current loaded dat fileprint(GEOIP.get_database_path())# info about internal cacheprint(GEOIP.cache_info())# clear the internal cacheprint(GEOIP.clear_cache())# to see the difference after clear cacheprint(GEOIP.cache_info())There is a method to pretty print the result as json.dumps():>>>result=MyGeoIP.lookup("191.251.128.1")>>>print(result.pp_json()){"ip":"191.251.128.1","country_code":"BR","country_name":"Brazil","city":{"name":"Araraquara","subdivision_code":"SP","subdivision_name":"Sao Paulo"},"cidr":"191.251.128.0/20","hostname":"","asn_name":"TELEFONICA BRASIL S.A","asn_cidr":"191.250.0.0/15","is_private":false,"elapsed_time":"0.000147356 sec"}or simply:result.pp_json(print_result=True)To see the start-up line without setverbose=True:>>>fromgeoip2fastimportGeoIP2Fast>>>G=GeoIP2Fast()>>>G.startup_line_text'GeoIP2Fast v1.2.0 is ready! geoip2fast.dat.gz loaded with 459.270 networks in 0.03393 seconds and using 25.25 MiB.'>>>Private/Reserved networks were included in the database just to be able to provide an answer if one of these IPs is searched. When it happens, the country_code will return "--", the "network name" will be displayed in the country_name and the range of that network will be displayed in the cidr property, and the propertyis_privateis setted toTrue.>>>fromgeoip2fastimportGeoIP2Fast>>>geoip=GeoIP2Fast(verbose=True)'GeoIP2Fast v1.2.0 is ready! geoip2fast.dat.gz loaded with 459.270 networks in 0.03393 seconds and using 25.25 MiB.'>>>>>>G.lookup("10.20.30.40"){'ip':'10.20.30.40','country_code':'--','country_name':'Private Network Class A','cidr':'10.0.0.0/8','hostname':'','asn_name':'IANA.ORG','asn_cidr':'10.0.0.0/8','is_private':True,'elapsed_time':'0.000015946 sec'}>>>G.lookup("169.254.10.20"){'ip':'169.254.10.20','country_code':'--','country_name':'APIPA Automatic Priv.IP Addressing','cidr':'169.254.0.0/16','hostname':'','asn_name':'IANA.ORG','asn_cidr':'169.254.0.0/16','is_private':True,'elapsed_time':'0.000093106 sec'}>>>You can change the behavior of what will be returned in country_code property of "private networks" and for "networks not found":>>>fromgeoip2fastimportGeoIP2Fast>>>G=GeoIP2Fast()>>>G.set_error_code_private_networks("@@")'@@'>>>G.lookup("10.20.30.40"){'ip':'10.20.30.40','country_code':'@@','country_name':'Private Network Class A','cidr':'10.0.0.0/8','hostname':'','asn_name':'IANA.ORG','asn_cidr':'10.0.0.0/8','is_private':True,'elapsed_time':'0.000144236 sec'}>>>>>>G.set_error_code_network_not_found("##")'##'>>>G.lookup("23.142.129.0"){'ip':'23.142.129.0','country_code':'##','country_name':'<not found in database>','cidr':'','hostname':'','asn_name':'','asn_cidr':'','is_private':False,'elapsed_time':'0.000012352 sec'}>>>You can use it as a CLI also from any path:# geoip2fastGeoIP2Fastv1.2.0Usage:geoip2fast.py[-h][-v][-d]<ip_address_1>,<ip_address_2>,<ip_address_N>,...# geoip2fast 9.9.9.9,15.20.25.30 -d{"ip":"9.9.9.9","country_code":"US","country_name":"United States","cidr":"9.9.9.9/32","hostname":"dns9.quad9.net","asn_name":"","asn_cidr":"","is_private":false,"elapsed_time":"0.000034214 sec","elapsed_time_hostname":"0.001079759 sec"}{"ip":"15.20.25.30","country_code":"US","country_name":"United States","cidr":"15.0.0.0/10","hostname":"<Unknown host>","asn_name":"","asn_cidr":"","is_private":false,"elapsed_time":"0.000022537 sec"}# geoip2fast "2.3.4.5, 4.5.6.7, 8.9.10.11" | jq -r '.country_code'FR
US
US# geoip2fast 8.8.8.8,1.1.1.1,200.204.0.10 -d | jq -r '.hostname'dns.google
one.one.one.one
resolver1.telesp.net.brHow fast is it?With an virtual machine with 1 CPU and 4Gb of RAM, we have lookupslower than 0,00003 seconds. And if the lookup still in library´s internal cache, the elapsed time goes down to 0,000003 seconds.GeoIP2Fast can do more than 100K queries per second, per core. It takes less than 0,07 seconds to load the datafile into memory and get ready to lookup. Useverbose=Trueto create the object GeoIP2Fast to see the spent time to start.Now some tests are included in the geoip2fast.py file.# geoip2fast -hGeoIP2Fastv1.2.1Usage:geoip2fast.py[-h][-v][-d]<ip_address_1>,<ip_address_2>,<ip_address_N>,...

Testsparameters:--self-testStartsaself-testwithsomerandomicIPaddresses.--self-test-cityStartsaself-testwithsomerandomicIPaddressesandwithcitynamessupport.--speed-testDoaspeedtestwith1milliononrandomicIPaddresses.--random-testStartatestwith1.000.000ofrandomicIPsandcalculatealookupaveragetime.--coverage[-v]ShowsastatisticofhowmanyIPsarecoveredbycurrentdatfile.--missing-ips[-v]PrintallIPnetworksthatdoesn't have geo information (only for IPv4).Automatic update:--update-all [-v]    Download all dat.gz files available in the repository below:https://github.com/rabuchaim/geoip2fast/releases/download/LATEST/--update-file <geoip2fast_dat_filename> [-v]Download a specific filename from the repository. Only one file is allowed.Allowed values are: geoip2fast.dat.gz OR geoip2fast-ipv6.dat.gz ORgeoip2fast-asn.dat.gz OR geoip2fast-asn-ipv6.dat.gz ORgeoip2fast-city.dat.gz OR geoip2fast-city-ipv6.dat.gz ORgeoip2fast-city-asn.dat.gz OR geoip2fast-city-asn-ipv6.dat.gz--dest <a directory path or a filename> [-v]Specify the destination directory for the downloaded files. When combined withthe '--update-file'parameter,youcanspecifyanexistingdirectory,withorwithoutafilename,oryoucanjustspecifyafilename.Intheabsenceofthisparameterordirectoryinformation,thelibrarydirectorywillbeusedasdefaultvalue.Thisparameterisoptional.Thefilenamemustendwith.dat.gzextension.Usetheverboseparameter(-v)ifyouwanttoseethedownloadprogress,otherwisetherewillbenooutput.Youalsohavetousethisparametertoviewpossibleerrorsinyourconsole.

Moreoptions:-dResolvetheDNSofgivenIPaddress.-hShowthishelptext.-vVerbosemode.-vvvShowsthelocationofcurrentdatfileinuse.geoip2fast --self-test# geoip2fast --self-testGeoIP2Fastv1.2.0isready!geoip2fast.dat.gzloadedwith459.270networksin0.09268secondsandusing71.04MiB.

Startingaself-test...

>266.266.266.266<invalidipaddress>[0.000018189sec]Cached>[0.000003038sec]>192,0x0/32<invalidipaddress>[0.000002468sec]Cached>[0.000001868sec]>10.20.30.40--PrivateNetworkClassA[0.000020032sec]Cached>[0.000001231sec]IANA.ORG
>38.174.13.98CACanada[0.000015079sec]Cached>[0.000001414sec]AS-NSL-261
>102.195.139.98--<notfoundindatabase>[0.000004650sec]Cached>[0.000000471sec]>139.5.38.161INIndia[0.000012635sec]Cached>[0.000000845sec]FivenetworkBroadbandSolutionPvtLtd
>4.219.112.222NONorway[0.000009926sec]Cached>[0.000000727sec]MICROSOFT-CORP-MSN-AS-BLOCK
>137.44.167.24GBUnitedKingdom[0.000009575sec]Cached>[0.000000663sec]JiscServicesLimited
>28.92.226.103USUnitedStates[0.000009599sec]Cached>[0.000000605sec]DNIC-AS-00749
>180.115.120.126CNChina[0.000010468sec]Cached>[0.000000702sec]Chinanet(.....)geoip2fast --speed-test# geoip2fast --speed-testGeoIP2Fastv1.2.0isready!geoip2fast.dat.gzloadedwith459.270networksin0.09292secondsandusing71.05MiB.

Calculatingcurrentspeed...waitafewsecondsplease...

Currentspeed:143159.49lookupspersecond(1.000.000IPswithanaverageof0.000006985secondsperlookup)[6.98522sec]geoip2fast --coverage# geoip2fast --coverageGeoIP2Fastv1.2.0isready!geoip2fast.dat.gzloadedwith4.488.723networksin0.47685secondsandusing408.66MiB.

Usetheparameter'-v'toseeallnetworksincludedinyour/opt/geoip2fast/geoip2fast/geoip2fast.dat.gzfile.

CurrentIPv4coverage:99.64%(4.279.552.038IPv4in3.246.406networks)[0.59747sec]CurrentIPv6coverage:0.40%(1.364.444.943.175.748.876.962.337.373.462.462.464IPv6in1.242.317networks)[0.59747sec]geoip2fast --coverage -v# geoip2fast --coverage -vGeoIP2Fastv1.2.0isready!geoip2fast.dat.gzloadedwith753.871networksin0.13392secondsandusing103.54MiB.

Usetheparameter'-v'toseeallnetworksincludedinyour/usr/local/lib/python3.11/dist-packages/geoip2fast/geoip2fast.dat.gzfile.

-Network:0.0.0.0/8IPs:16777216--Reservedforselfidentification0.000042983sec
-Network:1.0.0.0/24IPs:256AUAustralia0.000017615sec
-Network:1.0.1.0/24IPs:256CNChina0.000011633sec
-Network:1.0.2.0/23IPs:512CNChina0.000009464sec
-Network:1.0.4.0/22IPs:1024AUAustralia0.000008925sec(.....)-Network:2c0f:ffc0::/32IPs:79228162514264337593543950336ZASouthAfrica0.000026057sec
-Network:2c0f:ffc8::/32IPs:79228162514264337593543950336ZASouthAfrica0.000028565sec
-Network:2c0f:ffd0::/32IPs:79228162514264337593543950336ZASouthAfrica0.000026283sec
-Network:2c0f:ffd8::/32IPs:79228162514264337593543950336ZASouthAfrica0.000026832sec
-Network:2c0f:ffe8::/32IPs:79228162514264337593543950336NGNigeria0.000027137sec
-Network:2c0f:fff0::/32IPs:79228162514264337593543950336NGNigeria0.000028008sec
-Network:fd00::/8IPs:1329227995784915872903807060280344576--ReservedforUniqueLocalAddresses0.000031203sec

CurrentIPv4coverage:99.64%(4.279.420.966IPv4in459.270networks)[64.13669sec]CurrentIPv6coverage:0.40%(1.364.444.943.175.748.876.962.337.373.462.462.464IPv6in294.601networks)[64.13669sec]geoip2fast --missing-ips# geoip2fast --missing-ipsGeoIP2Fastv1.2.0isready!geoip2fast.dat.gzloadedwith753.871networksin0.13392secondsandusing103.54MiB.

SearchingformissingIPs...From1.34.65.179to1.34.65.179>Network1.34.65.180/32>MissingIPs:1From1.46.23.235to1.46.23.235>Network1.46.23.236/32>MissingIPs:1From2.12.211.171to2.12.211.171>Network2.12.211.172/32>MissingIPs:1(.....)From220.158.148.0to220.158.151.255>Network220.158.152.0/22>MissingIPs:1024From223.165.0.0to223.165.3.255>Network223.165.4.0/22>MissingIPs:1024>>>ValidIPaddresseswithoutgeoinformation:15.551.441(0.36%ofallIPv4)[27.17811sec]Some IPs are excluded as described in page "Do Not Sell My Personal Information Requests" at Maxmind website.GeoIP2Dat - update geoip2fast.dat.gz file anytimeThe updates of geoip2fast.dat.gz file will be published twice a week on Githubhttps://github.com/rabuchaim/geoip2fast/releases/LATEST. You can also create your own dat file whenever you want, see instructions below.Download the Geolite2 (COUNTRY, CITY and ASN) CSV files from Maxmind website and place it into some diretory (in this example, was placed into/opt/maxmind/). Extract all zip files in this directory and rungeoip2datto see the options.The options (--country-dirOR--city-dir) AND--output-dirare mandatory. Specify the path of extracted files in--country-diror--city-diroption. And for--output-dir, put the current path./.If you want to add support for ASN data, add the option--asn-dir. And if you want to add IPv6 support, just add--with-ipv6to your command line.You can choose the language of country locations. The default isen.After creation ofgeoip2dat.dat.gzfile, move or copy this file to the directory of your application or to the directory of GeoIP2Fast library. You choose.From now you don't depend on anyone to have your data file updated.There's no point the code being open-source if you're dependent of a single file.The Philosophers call it 'Libertas'Automatic update of dat.gz filesFrom version v1.2.1 onwards, it is now possible to update the dat.gz files that were made available in our releases repository. You can update via command line or via code.Download the file "geoip2fast-asn-ipv6.dat.gz" and save it as "geoip2fast.dat.gz":>>>fromgeoip2fastimportGeoIP2Fast>>>G=GeoIP2Fast(verbose=True)GeoIP2Fastv1.2.1isready!geoip2fast.dat.gzloadedwith459270networksin0.03297secondsandusing25.12MiB.>>>update_result=G.update_file('geoip2fast-asn-ipv6.dat.gz','geoip2fast.dat.gz',verbose=False)>>>G.reload_data(verbose=True)GeoIP2Fastv1.2.1isready!geoip2fast.dat.gzloadedwith753871networksin0.12917secondsandusing113.54MiBTrue>>>Update all files:>>>fromgeoip2fastimportGeoIP2Fast>>>G=GeoIP2Fast()>>>update_result=G.update_all(verbose=True)-OpeningURLhttps://github.com/rabuchaim/geoip2fast/releases/download/LATEST/geoip2fast.dat.gz-LastModifiedDate:Mon,27Nov202302:40:08GMT-Downloadinggeoip2fast.dat.gz...100.00%of1.06MiB[6.51MiB/s][0.163sec]-Filesavedto:/opt/geoip2fast/geoip2fast-legacy/geoip2fast/geoip2fast.dat.gz-OpeningURLhttps://github.com/rabuchaim/geoip2fast/releases/download/LATEST/geoip2fast-ipv6.dat.gz-LastModifiedDate:Mon,27Nov202302:40:08GMT-Downloadinggeoip2fast-ipv6.dat.gz...100.00%of1.73MiB[9.66MiB/s][0.179sec]-Filesavedto:/opt/geoip2fast/geoip2fast-legacy/geoip2fast/geoip2fast-ipv6.dat.gz-OpeningURLhttps://github.com/rabuchaim/geoip2fast/releases/download/LATEST/geoip2fast-asn.dat.gz-LastModifiedDate:Mon,27Nov202302:40:08GMT-Downloadinggeoip2fast-asn.dat.gz...100.00%of3.06MiB[8.63MiB/s][0.354sec]-Filesavedto:/opt/geoip2fast/geoip2fast-legacy/geoip2fast/geoip2fast-asn.dat.gz-OpeningURLhttps://github.com/rabuchaim/geoip2fast/releases/download/LATEST/geoip2fast-asn-ipv6.dat.gz-LastModifiedDate:Mon,27Nov202302:40:08GMT-Downloadinggeoip2fast-asn-ipv6.dat.gz...100.00%of4.09MiB[7.66MiB/s][0.534sec]-Filesavedto:/opt/geoip2fast/geoip2fast-legacy/geoip2fast/geoip2fast-asn-ipv6.dat.gz>>>Update all files silently and verify if there are errors:>>>fromgeoip2fastimportGeoIP2Fast>>>G=GeoIP2Fast()>>>update_result=G.update_all(verbose=False)>>>errors_result=[itemforiteminupdate_resultifitem['error']isnotNone]>>>print(errors_result)[]You can change the update URL if you want.>>> G.get_update_url()
'https://github.com/rabuchaim/geoip2fast/releases/download/LATEST/'
>>> G.set_update_url("https://github.com/YOUR_OWN_REPO/YOUR_PROJECT/releases/download/latest/")
True
>>> G.get_update_url()
'https://github.com/YOUR_OWN_REPO/YOUR_PROJECT/releases/download/latest/'
>>>Update the file "geoip2fast-asn-ipv6.dat.gz" and overwrite "geoip2fast.dat.gz" and print the result.>>>fromgeoip2fastimportGeoIP2Fast>>>G=GeoIP2Fast(verbose=True)GeoIP2Fastv1.2.1isready!geoip2fast.dat.gzloadedwith459270networksin0.04414secondsandusing5.02MiB.>>>update_result=G.update_file('geoip2fast-asn-ipv6.dat.gz','geoip2fast.dat.gz',verbose=False)>>>frompprintimportpprintaspp>>>pp(update_result,sort_dicts=False){'error':None,'url':'https://github.com/rabuchaim/geoip2fast/releases/download/LATEST/geoip2fast-asn-ipv6.dat.gz','remote_filename':'geoip2fast-asn-ipv6.dat.gz','last_modified_date':'Mon, 27 Nov 2023 02:40:08 GMT','file_size':4289564,'file_destination':'/opt/geoip2fast/geoip2fast-legacy/geoip2fast/geoip2fast.dat.gz','average_download_speed':'4.09 MiB/sec','elapsed_time':'0.587267'}>>>>>>G.reload_data(verbose=True)GeoIP2Fastv1.2.1isready!geoip2fast.dat.gzloadedwith753871networksin0.12245secondsandusing57.55MiB.>>>Using the command line, no message will be displayed on the console unless you use the -v parameterUpdate all files via command line and save them in '/tmp/' directory:# geoip2fast --update-all --dest /tmp/ -v-OpeningURLhttps://github.com/rabuchaim/geoip2fast/releases/download/LATEST/geoip2fast.dat.gz
-LastModifiedDate:Mon,27Nov202302:40:08GMT
-Downloadinggeoip2fast.dat.gz...100.00%of1.06MiB[10.41MiB/s][0.102sec]-Filesavedto:/tmp/geoip2fast.dat.gz

-OpeningURLhttps://github.com/rabuchaim/geoip2fast/releases/download/LATEST/geoip2fast-ipv6.dat.gz
-LastModifiedDate:Mon,27Nov202302:40:08GMT
-Downloadinggeoip2fast-ipv6.dat.gz...100.00%of1.73MiB[9.46MiB/s][0.183sec]-Filesavedto:/tmp/geoip2fast-ipv6.dat.gz

-OpeningURLhttps://github.com/rabuchaim/geoip2fast/releases/download/LATEST/geoip2fast-asn.dat.gz
-LastModifiedDate:Mon,27Nov202302:40:08GMT
-Downloadinggeoip2fast-asn.dat.gz...100.00%of3.06MiB[7.06MiB/s][0.433sec]-Filesavedto:/tmp/geoip2fast-asn.dat.gz

-OpeningURLhttps://github.com/rabuchaim/geoip2fast/releases/download/LATEST/geoip2fast-asn-ipv6.dat.gz
-LastModifiedDate:Mon,27Nov202302:40:08GMT
-Downloadinggeoip2fast-asn-ipv6.dat.gz...100.00%of4.09MiB[7.31MiB/s][0.560sec]-Filesavedto:/tmp/geoip2fast-asn-ipv6.dat.gzUpdate the file "geoip2fast-asn-ipv6.dat.gz" and overwrite "geoip2fast.dat.gz"# geoip2fast --update-file geoip2fast-asn-ipv6.dat.gz --dest geoip2fast.dat.gz -v-OpeningURLhttps://github.com/rabuchaim/geoip2fast/releases/download/LATEST/geoip2fast-asn-ipv6.dat.gz
-LastModifiedDate:Mon,27Nov202302:40:08GMT
-Downloadinggeoip2fast-asn-ipv6.dat.gz...100.00%of4.09MiB[4.29MiB/s][0.954sec]-Filesavedto:/opt/geoip2fast/geoip2fast-legacy/geoip2fast/geoip2fast.dat.gzUpdate the file "geoip2fast.dat.gz" and save it in the library path# geoip2fast --update-file geoip2fast.dat.gz -v-OpeningURLhttps://github.com/rabuchaim/geoip2fast/releases/download/LATEST/geoip2fast.dat.gz
-LastModifiedDate:Mon,27Nov202302:40:08GMT
-Downloadinggeoip2fast.dat.gz...100.00%of1.06MiB[9.54MiB/s][0.111sec]-Filesavedto:/opt/geoip2fast/geoip2fast-legacy/geoip2fast/geoip2fast.dat.gzUpdate the file "geoip2fast.dat.gz" and save it in the library path# geoip2fast --update-file geoip2fast.dat.gz# echo $?0An example of a simulated download failure:# geoip2fast.py --update-file geoip2fast.dat.gz -v-OpeningURLhttps://github.com/rabuchaim/geoip2fast/releases/download/A_LATEST/geoip2fast.dat.gz
-Error:HTTPError404:NotFound-https://github.com/rabuchaim/geoip2fast/releases/download/A_LATEST/geoip2fast.dat.gz# echo $?1# geoip2fast.py --update-file geoip2fast.dat.gz -v-OpeningURLhttps://github.com/rabuchaim/geoip2fast/releases/download/LATEST/geoip2fast.dat.gz
-LastModifiedDate:Mon,27Nov202302:40:08GMT
PermissionError:[Errno1]Operationnotpermitted:'/opt/geoip2fast/geoip2fast-legacy/geoip2fast/geoip2fast.dat.gz'# echo $?1Using your own networksI´m planning to create an option to include your own networks in the database. For example, imagine you have a large private network on AWS, and each subnet is in a different region. You will be able to include them in the database to use GeoIP2Fast in your application. For now, you can edit thereservedNetworksvariable at the beginning of thegeoip2dat.pyfile and then generate your own data file.Suggestions and ideas are welcome. The initial idea is to include an option--include-networksingeoip2dat.pywhere you enter a json file.Create your own GeoIP CLI with 6 linesCreate a file namedgeoipcli.pyand save it in your home directory with the text below:#!/usr/bin/env python3importos,sys,geoip2fastiflen(sys.argv)>1andsys.argv[1]isnotNone:geoip2fast.GeoIP2Fast().lookup(sys.argv[1]).pp_json(print_result=True)else:print(f"Usage:{os.path.basename(__file__)}<ip_address>")Give execution permisstion to your file and create a symbolic link to your new file into/usr/sbinfolder, like this (let's assume that you saved this file into directory/root)chmod750/root/geoipcli.py
ln-s/root/geoipcli.py/usr/sbin/geoipcliNow, you just need to callgeoipclifrom any path.# geoipcliUsage:geoipcli<ip_address># geoipcli 1.2.3.4{"ip":"1.2.3.4","country_code":"AU","country_name":"Australia","cidr":"1.2.3.0/24","hostname":"","is_private":false,"elapsed_time":"0.000019727 sec"}GeoIP libraries that inspired meGeoIP2Nation -https://pypi.org/project/geoip2nation/(Created by Avi Asher)This library uses sqlite3 in-memory tables and use the same search concepts as GeoIP2Fast (based on search by the first´s IPs). Simple and fast! Unfortunately it is no longer being updated and that is why I developed GeoIP2Fast.GeoIP2 -https://pypi.org/project/geoip2/(Created by Maxmind)This is the best library to work with Maxmind paid subscription or with the free version. You can use http requests to Maxmind services or work with local Maxmind MMDB binary files. Pretty fast too. Sign-up to have access to all files of the free versionhttps://dev.maxmind.com/geoip/geolite2-free-geolocation-data* Maxmind is a registered trademark-https://www.maxmind.comTO DO lista pure-python version for REDIS with a very small footprint (pure protocol, won´t use any REDIS library)<<< On the way athttps://github.com/rabuchaim/geoip2redisa GeoIP Server;<<< On the way a docker container, your own GeoIP server inside your network. With rest API or socket (super fast)a mod_geoip2fast for NGINX;a better manual, maybe at readthedocs.io;Done in v1.1.10/v1.2.1- automatic update of dat.gz files;Done in v1.2.0- a version with cities;Done in v1.1.0-IPv6 support.Done in v1.0.5-a version with ASN.Done in v1.0.2-provide a script to update the base. If you have the paid subscription of Maxmind, you can download the files, extract into some directory and use this script to create your own geoip2fast.dat.gz file with the most complete, reliable and updated GeoIP information.Sugestions, feedbacks, bugs, wrong locations...E-mail me: ricardoabuchaim at gmail.com