<s> """<STR_LIT>""" <EOL> from scapy . data import KnowledgeBase <EOL> from scapy . config import conf <EOL> conf . IPCountry_base = "<STR_LIT>" <EOL> conf . countryLoc_base = "<STR_LIT>" <EOL> class IPCountryKnowledgeBase ( KnowledgeBase ) : <EOL> """<STR_LIT>""" <EOL> def lazy_init ( self ) : <EOL> self . base = load_object ( self . filename ) <EOL> class CountryLocKnowledgeBase ( KnowledgeBase ) : <EOL> def lazy_init ( self ) : <EOL> f = open ( self . filename ) <EOL> self . base = { } <EOL> while <NUM_LIT:1> : <EOL> l = f . readline ( ) <EOL> if not l : <EOL> break <EOL> l = l . strip ( ) . split ( "<STR_LIT:U+002C>" ) <EOL> if len ( l ) != <NUM_LIT:3> : <EOL> continue <EOL> c , lat , long = l <EOL> self . base [ c ] = ( float ( long ) , float ( lat ) ) <EOL> f . close ( ) <EOL> @ conf . commands . register <EOL> def locate_ip ( ip ) : <EOL> """<STR_LIT>""" <EOL> ip = map ( int , ip . split ( "<STR_LIT:.>" ) ) <EOL> ip = ip [ <NUM_LIT:3> ] + ( ip [ <NUM_LIT:2> ] << <NUM_LIT:8> ) + ( ip [ <NUM_LIT:1> ] << <NUM_LIT:16> ) + ( ip [ <NUM_LIT:0> ] << <NUM_LIT> ) <EOL> cloc = country_loc_kdb . get_base ( ) <EOL> db = IP_country_kdb . get_base ( ) <EOL> d = <NUM_LIT:0> <EOL> f = len ( db ) - <NUM_LIT:1> <EOL> while ( f - d ) > <NUM_LIT:1> : <EOL> guess = ( d + f ) / <NUM_LIT:2> <EOL> if ip > db [ guess ] [ <NUM_LIT:0> ] : <EOL> d = guess <EOL> else : <EOL> f = guess <EOL> s , e , c = db [ guess ] <EOL> if s <= ip and ip <= e : <EOL> return cloc . get ( c , None ) <EOL> conf . IP_country_kdb = IPCountryKnowledgeBase ( conf . IPCountry_base ) <EOL> conf . country_loc_kdb = CountryLocKnowledgeBase ( conf . countryLoc_base ) </s>
