python pygeoip example

Solutions on MaxInterview for python pygeoip example by the best coders in the world

showing results for - "python pygeoip example"
Maria
30 Jul 2019
1from flask import Flask, request, jsonify
2import pygeoip, json
3
4app = Flask(__name__)
5
6geo = pygeoip.GeoIP('GeoLiteCity.dat', pygeoip.MEMORY_CACHE)
7
8@app.route('/')
9def index():
10    client_ip = request.remote_addr
11    geo_data = geo.record_by_addr(client_ip)
12    return json.dumps(geo_data, indent=2) + '\n'
13
14if __name__ == '__main__':
15    app.run(host='0.0.0.0', port=80, debug=False)
16
Alejandro
07 Feb 2019
1def geoip(inp):
2    "geoip <host/ip> -- Gets the location of <host/ip>"
3    try:
4        record = geo.record_by_name(inp)
5    except:
6        return "Sorry, I can't locate that in my database."
7
8    data = {}
9
10    if "region_name" in record:
11        # we try catching an exception here because the region DB is missing a few areas
12        # it's a lazy patch, but it should do the job
13        try:
14            data["region"] = ", " + regions[record["country_code"]][record["region_name"]]
15        except:
16            data["region"] = ""
17    else:
18        data["region"] = ""
19
20    data["cc"] = record["country_code"] or "N/A"
21    data["country"] = record["country_name"] or "Unknown"
22    data["city"] = record["city"] or "Unknown"
23    return formatting.output('GeoIP', ['\x02Country:\x02 {country} ({cc}) \x02City:\x02 {city}{region}'.format(**data)]) 
Damián
10 Apr 2017
1>>> gi = pygeoip.GeoIP('GeoIPRegion.dat')
2>>> gi.region_by_name('apple.com')
3{'region_code': 'CA', 'country_code': 'US'}
4
Debora
17 Apr 2017
1>>> gi = pygeoip.GeoIP('GeoIPISP.dat')
2>>> gi.isp_by_name('cnn.com')
3'Turner Broadcasting System'
4
Jessica
21 Mar 2019
1def geo_ip(host):
2
3    try:
4
5       rawdata = pygeoip.GeoIP('GeoLiteCity.dat')
6       data = rawdata.record_by_name(host)	
7       country = data['country_name']
8       city = data['city']
9       longi = data['longitude']
10       lat = data['latitude']
11       time_zone = data['time_zone']
12       area_code = data['area_code']
13       country_code = data['country_code']
14       region_code = data['region_code']
15       dma_code = data['dma_code']
16       metro_code = data['metro_code']
17       country_code3 = data['country_code3']
18       zip_code = data['postal_code']
19       continent = data['continent']
20
21       print '[*] IP Address: ',host
22       print '[*] City: ',city
23       print '[*] Region Code: ',region_code
24       print '[*] Area Code: ',area_code
25       print '[*] Time Zone: ',time_zone
26       print '[*] Dma Code: ',dma_code
27       print '[*] Metro Code: ',metro_code
28       print '[*] Latitude: ',lat
29       print '[*] Longitude: ',longi
30       print '[*] Zip Code: ',zip_code
31       print '[*] Country Name: ',country
32       print '[*] Country Code: ',country_code
33       print '[*] Country Code3: ',country_code3
34       print '[*] Continent: ',continent
35
36    except :
37           print "[*] Please verify your ip !" 
Cristal
31 Jul 2020
1def geo_ip(res_type, ip):
2    try:
3        import pygeoip
4        gi = pygeoip.GeoIP('GeoIP.dat')
5        if res_type == 'name':
6            return gi.country_name_by_addr(ip)
7        if res_type == 'cc':
8            return gi.country_code_by_addr(ip)
9        return gi.country_code_by_addr(ip)
10    except Exception as e:
11        print e
12        return ''
13
14#----------------------------------------------------------------------
15# Search
16#---------------------------------------------------------------------- 
Lucien
05 Jul 2018
1def main(argv):
2    parseargs(argv)
3    print(BANNER.format(APP_NAME, VERSION))
4
5    print("[+] Resolving host...")
6    host = gethostaddr()
7    if (host is None or not host):
8       print("[!] Unable to resolve host {}".format(target))
9       print("[!] Make sure the host is up: ping -c1 {}\n".format(target))
10       sys.exit(0)
11
12    print("[+] Host {} has address: {}".format(target, host))
13    print("[+] Tracking host...")
14
15    query = pygeoip.GeoIP(DB_FILE)
16    result = query.record_by_addr(host)
17
18    if (result is None or not result):
19        print("[!] Host location not found")
20        sys.exit(0)
21
22    print("[+] Host location found:")
23    print json.dumps(result, indent=4, sort_keys=True, ensure_ascii=False, encoding="utf-8") 
Cherry
25 Aug 2018
1def count_ref_ipPerAsn(ref=None):
2
3    gi = pygeoip.GeoIP("../lib/GeoIPASNum.dat")
4    
5    if ref is None:
6        ref = pickle.load(open("./saved_references/56d9b1eab0ab021d00224ca8_routeChange.pickle","r"))
7
8    ipPerAsn = defaultdict(set)
9
10    for targetRef in ref.values(): 
11        for router, hops in targetRef.iteritems():
12            for hop in hops.keys():
13                if hop != "stats":
14                    ipPerAsn[asn_by_addr(hop,gi)[0]].add(hop)
15
16    return ipPerAsn 
Maylis
07 Jan 2020
1pip install pygeoip
2
Eoin
17 Jun 2016
1def _country_code_from_ip(ip_addr):
2    """
3    Return the country code associated with an IP address.
4    Handles both IPv4 and IPv6 addresses.
5
6    Args:
7        ip_addr (str): The IP address to look up.
8
9    Returns:
10        str: A 2-letter country code.
11
12    """
13    if ip_addr.find(':') >= 0:
14        return pygeoip.GeoIP(settings.GEOIPV6_PATH).country_code_by_addr(ip_addr)
15    else:
16        return pygeoip.GeoIP(settings.GEOIP_PATH).country_code_by_addr(ip_addr)