Answers for "python pygeoip example"

1

python pygeoip example

from flask import Flask, request, jsonify
import pygeoip, json

app = Flask(__name__)

geo = pygeoip.GeoIP('GeoLiteCity.dat', pygeoip.MEMORY_CACHE)

@app.route('/')
def index():
    client_ip = request.remote_addr
    geo_data = geo.record_by_addr(client_ip)
    return json.dumps(geo_data, indent=2) + '\n'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=False)
Posted by: Guest on May-13-2020
1

python pygeoip example

def geoip(inp):
    "geoip <host/ip> -- Gets the location of <host/ip>"
    try:
        record = geo.record_by_name(inp)
    except:
        return "Sorry, I can't locate that in my database."

    data = {}

    if "region_name" in record:
        # we try catching an exception here because the region DB is missing a few areas
        # it's a lazy patch, but it should do the job
        try:
            data["region"] = ", " + regions[record["country_code"]][record["region_name"]]
        except:
            data["region"] = ""
    else:
        data["region"] = ""

    data["cc"] = record["country_code"] or "N/A"
    data["country"] = record["country_name"] or "Unknown"
    data["city"] = record["city"] or "Unknown"
    return formatting.output('GeoIP', ['\x02Country:\x02 {country} ({cc}) \x02City:\x02 {city}{region}'.format(**data)])
Posted by: Guest on May-13-2020
0

python pygeoip example

def geo_ip(res_type, ip):
    try:
        import pygeoip
        gi = pygeoip.GeoIP('GeoIP.dat')
        if res_type == 'name':
            return gi.country_name_by_addr(ip)
        if res_type == 'cc':
            return gi.country_code_by_addr(ip)
        return gi.country_code_by_addr(ip)
    except Exception as e:
        print e
        return ''

#----------------------------------------------------------------------
# Search
#----------------------------------------------------------------------
Posted by: Guest on May-13-2020
0

python pygeoip example

def main(argv):
    parseargs(argv)
    print(BANNER.format(APP_NAME, VERSION))

    print("[+] Resolving host...")
    host = gethostaddr()
    if (host is None or not host):
       print("[!] Unable to resolve host {}".format(target))
       print("[!] Make sure the host is up: ping -c1 {}\n".format(target))
       sys.exit(0)

    print("[+] Host {} has address: {}".format(target, host))
    print("[+] Tracking host...")

    query = pygeoip.GeoIP(DB_FILE)
    result = query.record_by_addr(host)

    if (result is None or not result):
        print("[!] Host location not found")
        sys.exit(0)

    print("[+] Host location found:")
    print json.dumps(result, indent=4, sort_keys=True, ensure_ascii=False, encoding="utf-8")
Posted by: Guest on May-13-2020
0

python pygeoip example

pip install pygeoip
Posted by: Guest on May-13-2020
0

python pygeoip example

>>> gi = pygeoip.GeoIP('GeoIPRegion.dat')
>>> gi.region_by_name('apple.com')
{'region_code': 'CA', 'country_code': 'US'}
Posted by: Guest on May-13-2020
0

python pygeoip example

def load_geoip():
    import pygeoip
    from pygeoip.const import MEMORY_CACHE
    
    resource_package = __name__
    filename = pkg_resources.resource_filename(resource_package, 'GeoIP.dat')
    return pygeoip.GeoIP(filename, flags=MEMORY_CACHE)
Posted by: Guest on May-13-2020
0

python pygeoip example

>>> gi = pygeoip.GeoIP('GeoIPISP.dat')
>>> gi.isp_by_name('cnn.com')
'Turner Broadcasting System'
Posted by: Guest on May-13-2020
0

python pygeoip example

def count_ref_ipPerAsn(ref=None):

    gi = pygeoip.GeoIP("../lib/GeoIPASNum.dat")
    
    if ref is None:
        ref = pickle.load(open("./saved_references/56d9b1eab0ab021d00224ca8_routeChange.pickle","r"))

    ipPerAsn = defaultdict(set)

    for targetRef in ref.values(): 
        for router, hops in targetRef.iteritems():
            for hop in hops.keys():
                if hop != "stats":
                    ipPerAsn[asn_by_addr(hop,gi)[0]].add(hop)

    return ipPerAsn
Posted by: Guest on May-13-2020
0

python pygeoip example

def _country_code_from_ip(ip_addr):
    """
    Return the country code associated with an IP address.
    Handles both IPv4 and IPv6 addresses.

    Args:
        ip_addr (str): The IP address to look up.

    Returns:
        str: A 2-letter country code.

    """
    if ip_addr.find(':') >= 0:
        return pygeoip.GeoIP(settings.GEOIPV6_PATH).country_code_by_addr(ip_addr)
    else:
        return pygeoip.GeoIP(settings.GEOIP_PATH).country_code_by_addr(ip_addr)
Posted by: Guest on May-13-2020

Python Answers by Framework

Browse Popular Code Answers by Language