|
| 1 | +from flask import Flask, request, logging |
| 2 | +from logging.handlers import RotatingFileHandler |
| 3 | +import csv |
| 4 | +from collections import namedtuple, OrderedDict |
| 5 | +import sys |
| 6 | +import os |
| 7 | +from tqdm import tqdm |
| 8 | +import numpy as np |
| 9 | +import time |
| 10 | +import json |
| 11 | +import ipaddress |
| 12 | + |
| 13 | + |
| 14 | +class ip2location_database: |
| 15 | + |
| 16 | + db = OrderedDict() |
| 17 | + db_keys = None |
| 18 | + ## IN THE ROW BELOW THE '_id' FIELD IS OMITTED BECAUSE NAMED TUPLES CANNOT HAVE FIELDS THAT BEGIN WITH AN UNDERSCORE |
| 19 | + ip2location_db_row = namedtuple('ip2location_db_row','ip_from ip_to country_code country_name region_name city_name latitude longitude zip_code time_zone') |
| 20 | + database_flatfile_path = None |
| 21 | + abs_min = None |
| 22 | + abs_max = None |
| 23 | + |
| 24 | + def __init__(self, db_name="/opt/IP2LocationService/IP2LOCATION-LITE-DB11.CSV"): |
| 25 | + self.database_flatfile_path = db_name |
| 26 | + print self.database_flatfile_path |
| 27 | + |
| 28 | + def read_database(self): |
| 29 | + try: |
| 30 | + first_line = True |
| 31 | + with open(self.database_flatfile_path, "rb") as fin: |
| 32 | + reader = csv.reader(fin) |
| 33 | + for row in tqdm(reader): |
| 34 | + self.db[int(row[1])] = self.ip2location_db_row(ip_from=int(row[0]), ip_to=int(row[1]), country_code=str(row[2]), country_name=str(row[3]), region_name=str(row[4]), city_name=str(row[5]), latitude=float(row[6]), longitude=float(row[7]), zip_code=str(row[8]), time_zone=str(row[9])) |
| 35 | + if first_line: |
| 36 | + first_line = False |
| 37 | + self.abs_min = int(row[0]) |
| 38 | + self.db_keys = np.array(self.db.keys()) |
| 39 | + self.abs_max = int(self.db_keys[-1]) |
| 40 | + except: |
| 41 | + print "Failed to read database. Please make sure that file exists and has a schema matching the one found at http://lite.ip2location.com/database/ip-country-region-city-latitude-longitude-zipcode-timezone" |
| 42 | + print sys.exc_info()[0] |
| 43 | + raise |
| 44 | + |
| 45 | + def set_database_path(self, db_name): |
| 46 | + self.database_flatfile_path = db_name |
| 47 | + |
| 48 | + def find_one_ip(self, ip_address_to_query="172.217.3.206"): |
| 49 | + if '.' in ip_address_to_query: |
| 50 | + ip_address_to_query = int(ipaddress.IPv4Address(unicode(ip_address_to_query))) |
| 51 | + else: |
| 52 | + ip_address_to_query = int(ip_address_to_query) |
| 53 | + |
| 54 | + if ip_address_to_query < self.abs_min: |
| 55 | + return "UNDEFINED" |
| 56 | + elif ip_address_to_query > self.abs_max: |
| 57 | + return "UNDEFINED" |
| 58 | + else: |
| 59 | + low = 0 |
| 60 | + mid = len(self.db)/2 |
| 61 | + high = len(self.db) |
| 62 | + iterations = 0 |
| 63 | + while True: |
| 64 | + iterations += 1 |
| 65 | + if ip_address_to_query > self.db_keys[mid]: |
| 66 | + low = mid |
| 67 | + mid = ((high - mid)/2) + mid |
| 68 | + elif ip_address_to_query >= self.db[self.db_keys[mid]].ip_from: |
| 69 | + print iterations |
| 70 | + return self.db[self.db_keys[mid]] |
| 71 | + else: |
| 72 | + high = mid |
| 73 | + mid = mid - ((mid - low)/2) |
| 74 | + |
| 75 | + def find_many_ips(self, ip_addresses_to_query=["172.217.3.206"]): |
| 76 | + min_curr = self.abs_min |
| 77 | + min_index = 0 |
| 78 | + ip_num_to_ip_map={} |
| 79 | + results = {} |
| 80 | + for i in xrange(len(ip_addresses_to_query)): |
| 81 | + if '.' in ip_addresses_to_query[i]: |
| 82 | + ip_num = int(ipaddress.IPv4Address(unicode(ip_addresses_to_query[i]))) |
| 83 | + ip_num_to_ip_map[ip_num] = ip_addresses_to_query[i] |
| 84 | + ip_addresses_to_query[i] = ip_num |
| 85 | + ip_addresses_to_query.sort() |
| 86 | + for item in self.db_keys: |
| 87 | + for i in xrange(min_index, len(ip_addresses_to_query)): |
| 88 | + if (ip_addresses_to_query[i] <= item) and (ip_addresses_to_query[i] >= min_curr): |
| 89 | + results[ip_addresses_to_query[i]] = self.db[item] |
| 90 | + else: |
| 91 | + min_index = i |
| 92 | + break |
| 93 | + return {ip_num_to_ip_map[results.keys()[i]]:results[results.keys()[i]] for i in xrange(len(results))} |
| 94 | + |
| 95 | +def daemonize(): |
| 96 | + """ |
| 97 | + do the UNIX double-fork magic, see Stevens' "Advanced |
| 98 | + Programming in the UNIX Environment" for details (ISBN 0201563177) |
| 99 | + http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 |
| 100 | + """ |
| 101 | + stdin = "/dev/null" |
| 102 | + stdout = "/dev/null" |
| 103 | + stderr = "/dev/null" |
| 104 | + try: |
| 105 | + pid = os.fork() |
| 106 | + if pid > 0: |
| 107 | + # exit first parent |
| 108 | + sys.exit(0) |
| 109 | + except OSError, e: |
| 110 | + sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)) |
| 111 | + sys.exit(1) |
| 112 | + |
| 113 | + # decouple from parent environment |
| 114 | + os.chdir("/") |
| 115 | + os.setsid() |
| 116 | + os.umask(0) |
| 117 | + |
| 118 | + # do second fork |
| 119 | + try: |
| 120 | + pid = os.fork() |
| 121 | + if pid > 0: |
| 122 | + # exit from second parent |
| 123 | + sys.exit(0) |
| 124 | + except OSError, e: |
| 125 | + sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) |
| 126 | + sys.exit(1) |
| 127 | + |
| 128 | + # redirect standard file descriptors |
| 129 | + sys.stdout.flush() |
| 130 | + sys.stderr.flush() |
| 131 | + si = file(stdin, 'r') |
| 132 | + so = file(stdout, 'a+') |
| 133 | + se = file(stderr, 'a+', 0) |
| 134 | + os.dup2(si.fileno(), sys.stdin.fileno()) |
| 135 | + os.dup2(so.fileno(), sys.stdout.fileno()) |
| 136 | + os.dup2(se.fileno(), sys.stderr.fileno()) |
| 137 | + |
| 138 | + # write pidfile |
| 139 | + # atexit.register(self.delpid) |
| 140 | + # pid = str(os.getpid()) |
| 141 | + # file(self.pidfile,'w+').write("%s\n" % pid) |
| 142 | + |
| 143 | +app = Flask(__name__) |
| 144 | +db = ip2location_database() |
| 145 | + |
| 146 | +@app.route("/lookup/", methods=['POST']) |
| 147 | +def find_ip(): |
| 148 | + result = {} |
| 149 | + args = request.get_json(force=True) |
| 150 | + result['type'] = "success" |
| 151 | + result['result'] = db.find_one_ip(args['ip'])._asdict() |
| 152 | + result['result']['_id'] = "" #this must be added here as namedtuples do not support fields with '_' in their name |
| 153 | + return json.dumps(result) |
| 154 | + |
| 155 | +@app.route("/ip2location/getcoor/<ip>", methods=['GET']) |
| 156 | +def emulate_reddys_service(ip): |
| 157 | + result = {} |
| 158 | + try: |
| 159 | + result['type'] = "success" |
| 160 | + result['result'] = db.find_one_ip(ip)._asdict() |
| 161 | + result['result']['_id'] = "" #this must be added here as namedtuples do not support fields with '_' in their name |
| 162 | + return json.dumps(result) |
| 163 | + except: |
| 164 | + result['type'] = "error" |
| 165 | + result['result'] = None |
| 166 | + return json.dumps(result) |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + daemonize() |
| 170 | + db.read_database() |
| 171 | + logger = logging.getLogger('werkzeug') |
| 172 | + handler = RotatingFileHandler('access.log', maxBytes=500) |
| 173 | + logger.addHandler(handler) |
| 174 | + app.run(host='0.0.0.0', port=5000) |
0 commit comments