本文整理汇总了Python中pygeoip.GeoIP.country_code_by_addr方法的典型用法代码示例。如果您正苦于以下问题:Python GeoIP.country_code_by_addr方法的具体用法?Python GeoIP.country_code_by_addr怎么用?Python GeoIP.country_code_by_addr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygeoip.GeoIP
的用法示例。
在下文中一共展示了GeoIP.country_code_by_addr方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: IPToLocation
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import country_code_by_addr [as 别名]
def IPToLocation(ipaddr):
from ooni.settings import config
country_file = config.get_data_file_path('GeoIP/GeoIP.dat')
asn_file = config.get_data_file_path('GeoIP/GeoIPASNum.dat')
location = {'city': None, 'countrycode': 'ZZ', 'asn': 'AS0'}
def error():
log.err("Could not find GeoIP data file in data directories."
"Try running ooniresources or"
" edit your ooniprobe.conf")
try:
country_dat = GeoIP(country_file)
location['countrycode'] = country_dat.country_code_by_addr(ipaddr)
if not location['countrycode']:
location['countrycode'] = 'ZZ'
except IOError:
error()
try:
asn_dat = GeoIP(asn_file)
location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0]
except:
error()
return location
示例2: IPToLocation
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import country_code_by_addr [as 别名]
def IPToLocation(ipaddr):
city_file = os.path.join(config.advanced.geoip_data_dir, 'GeoLiteCity.dat')
country_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIP.dat')
asn_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIPASNum.dat')
location = {'city': None, 'countrycode': None, 'asn': None}
try:
city_dat = GeoIP(city_file)
try:
location['city'] = city_dat.record_by_addr(ipaddr)['city']
except TypeError:
location['city'] = None
country_dat = GeoIP(country_file)
location['countrycode'] = country_dat.country_code_by_addr(ipaddr)
if not location['countrycode']:
location['countrycode'] = 'ZZ'
asn_dat = GeoIP(asn_file)
try:
location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0]
except AttributeError:
location['asn'] = 'AS0'
except IOError:
log.err("Could not find GeoIP data files. Go into %s "
"and run make geoip or change the geoip_data_dir "
"in the config file" % config.advanced.geoip_data_dir)
raise GeoIPDataFilesNotFound
return location
示例3: IPToLocation
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import country_code_by_addr [as 别名]
def IPToLocation(ipaddr):
from ooni.settings import config
city_file = os.path.join(config.advanced.geoip_data_dir, 'GeoLiteCity.dat')
country_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIP.dat')
asn_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIPASNum.dat')
location = {'city': None, 'countrycode': 'ZZ', 'asn': 'AS0'}
try:
country_dat = GeoIP(country_file)
location['countrycode'] = country_dat.country_code_by_addr(ipaddr)
if not location['countrycode']:
location['countrycode'] = 'ZZ'
except IOError:
log.err("Could not find GeoIP data file. Go into %s "
"and make sure GeoIP.dat is present or change the location "
"in the config file" % config.advanced.geoip_data_dir)
try:
city_dat = GeoIP(city_file)
location['city'] = city_dat.record_by_addr(ipaddr)['city']
except:
log.err("Could not find the city your IP is from. "
"Download the GeoLiteCity.dat file into the geoip_data_dir"
" or install geoip-database-contrib.")
try:
asn_dat = GeoIP(asn_file)
location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0]
except:
log.err("Could not find the ASN for your IP. "
"Download the GeoIPASNum.dat file into the geoip_data_dir"
" or install geoip-database-contrib.")
return location
示例4: IPToLocation
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import country_code_by_addr [as 别名]
def IPToLocation(ipaddr):
from ooni.settings import config
city_file = os.path.join(config.advanced.geoip_data_dir, 'GeoLiteCity.dat')
country_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIP.dat')
asn_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIPASNum.dat')
location = {'city': None, 'countrycode': 'ZZ', 'asn': 'AS0'}
def error():
log.err("Could not find GeoIP data file in %s."
"Try running ooniresources --update-geoip or"
" edit your ooniprobe.conf" % config.advanced.geoip_data_dir)
try:
country_dat = GeoIP(country_file)
location['countrycode'] = country_dat.country_code_by_addr(ipaddr)
if not location['countrycode']:
location['countrycode'] = 'ZZ'
except IOError:
error()
try:
city_dat = GeoIP(city_file)
location['city'] = city_dat.record_by_addr(ipaddr)['city']
except:
error()
try:
asn_dat = GeoIP(asn_file)
location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0]
except:
error()
return location
示例5: IPToLocation
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import country_code_by_addr [as 别名]
def IPToLocation(ipaddr):
from ooni.settings import config
country_file = config.get_data_file_path('GeoIP/GeoIP.dat')
asn_file = config.get_data_file_path('GeoIP/GeoIPASNum.dat')
location = {'city': None, 'countrycode': 'ZZ', 'asn': 'AS0'}
if not asn_file or not country_file:
log.err("Could not find GeoIP data file in data directories."
"Try running ooniresources or"
" edit your ooniprobe.conf")
return location
country_dat = GeoIP(country_file)
asn_dat = GeoIP(asn_file)
country_code = country_dat.country_code_by_addr(ipaddr)
if country_code is not None:
location['countrycode'] = country_code
asn = asn_dat.org_by_addr(ipaddr)
if asn is not None:
location['asn'] = asn.split(' ')[0]
return location
示例6: update_data
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import country_code_by_addr [as 别名]
def update_data(self, request, commit=True):
self.user_agent = request.META.get('HTTP_USER_AGENT', None)
geo = GeoIP(settings.GEOIP_DATABASE)
self.country_code = geo.country_code_by_addr(
request.META.get('REMOTE_ADDR', None)
)
self.visitor_ip = request.META.get('REMOTE_ADDR', None)
if hasattr(request, 'user') and request.user.is_authenticated():
self.visitor = request.user
if commit:
self.save()
示例7: ip_to_country
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import country_code_by_addr [as 别名]
def ip_to_country(ip_address):
db = GeoIP(
path.abspath(
path.join(
path.dirname(__file__),
'fixtures',
'geoip.dat'
)
),
MEMORY_CACHE
)
return Country.objects.get(
code = db.country_code_by_addr(ip_address)
)
示例8: IPToLocation
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import country_code_by_addr [as 别名]
def IPToLocation(ipaddr):
city_file = os.path.join(config.advanced.geoip_data_dir, 'GeoLiteCity.dat')
country_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIP.dat')
asn_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIPASNum.dat')
location = {'city': None, 'countrycode': None, 'asn': None}
try:
city_dat = GeoIP(city_file)
location['city'] = city_dat.record_by_addr(ipaddr)['city']
country_dat = GeoIP(country_file)
location['countrycode'] = country_dat.country_code_by_addr(ipaddr)
asn_dat = GeoIP(asn_file)
location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0]
except IOError:
log.err("Could not find GeoIP data files. Go into data/ "
"and run make geoip")
raise GeoIPDataFilesNotFound
return location
示例9: __call__
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import country_code_by_addr [as 别名]
def __call__(self, environ, start_response):
"""Invoke the Controller"""
# WSGIController.__call__ dispatches to the Controller method
# the request is routed to. This routing information is
# available in environ['pylons.routes_dict']
from pylons.i18n.translation import set_lang
set_lang(environ['pylons.routes_dict']['_lang'])
#figure out which map to display based on geoloc
from pygeoip import GeoIP
gi = GeoIP('/usr/share/GeoIP/GeoIP.dat') #GeoIP.GEOIP_MEMORY_CACHE)
country_code = gi.country_code_by_addr(str(environ['REMOTE_ADDR']))
#raise Exception('%s from %s'%(country_code,environ['REMOTE_ADDR']))
if not country_code or country_code.lower() in ['a2']:
country_code = config['global_conf']['default_country']
country_code=country_code.lower()
c.use_google_maps,c.freemap_url = freemap_url_from_country(country_code)
try:
return WSGIController.__call__(self, environ, start_response)
finally:
meta.Session.remove()
示例10: QueryManager
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import country_code_by_addr [as 别名]
#.........这里部分代码省略.........
self.processedserver+=1
gobject.idle_add(self.set_progressbar_fraction)
if None == self.filter or \
self.filter.does_filter_match_server(server):
gobject.idle_add(self.tab.addServer, server)
else:
self.filterdcount+=1 # server is not added but filterd
except Empty:
#no more threads in the queue break thread execution
self.gui_lock = threading.RLock()
with self.gui_lock:
self.threadcount -= 1
Log.log.debug('Thread:' + threading.current_thread().name + \
' finishes working and exiting')
if self.threadcount == 0: #last thread reached
Log.log.info('Thread:' + threading.current_thread().name + \
' notifying the coordinator thread that the queue ' \
+ 'processing is finished')
self.messageque.put('finished')
break
def pulse_progressbar_thread(self):
"""
This method runs as a background thread that pulse the progressbar
of self.tab every 0.1 seconds
"""
while True:
try:
message = self.pulsemessageque.get(True, 0.1)
if message == 'stop_pulse':
break
except Empty:
self.gui_lock = threading.RLock()
with self.gui_lock:
gobject.idle_add(self.pulse_progressbar)
def set_progressbar_fraction(self):
"""
Sets the progressbar fraction. Uses the total servercount and the
processed servercount values to calculate the fraction
"""
if not self.abort:
fraction = float(self.processedserver) / float(self.servercount)
bartext = None
if 1.0 == fraction:
bartext = 'finished getting server status - displaying ' \
+ str((self.servercount-self.filterdcount)) + \
' servers (' + str(self.filterdcount) + ' filtered)'
self.tab.statusbar.progressbar.set_fraction(0.0)
else:
bartext = 'fetching server status (' + str(self.processedserver) + \
' / ' + str(self.servercount) + ') - ' + \
str(self.filterdcount) + ' servers filtered'
self.tab.statusbar.progressbar.set_fraction(fraction)
self.tab.statusbar.progressbar.set_text(bartext)
def pulse_progressbar(self):
"""
Pulse the progressbar, called by the thread using gobject.idle_add
"""
self.tab.statusbar.progressbar.set_text('fetching serverlist from master server')
self.tab.statusbar.progressbar.pulse()
def set_progressbar_aborted(self):
"""
Sets the text of the progressbar to the aborted message and resets fraction
"""
self.tab.statusbar.progressbar.set_text('task aborted')
self.tab.statusbar.progressbar.set_fraction(0.0)
def abort_current_task(self):
"""
Stops the processing of the queue by setting a abort flag.
"""
self.gui_lock = threading.RLock()
with self.gui_lock:
self.abort = True
def set_location(self, server):
"""
Determine location of a server based on the ip adress of the server
and set it at the server object
Extra threading lock used because there was some strange effects
without it.
@param - the server object
"""
self.geo_lock = threading.RLock()
with self.geo_lock:
#location = country(server.getHost())
location = self.pygeoip.country_code_by_addr(server.getHost())
locname = self.pygeoip.country_name_by_addr(server.getHost())
server.set_location(location)
server.set_location_name(locname)
示例11: show
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import country_code_by_addr [as 别名]
def show():
g = GeoIP('pygeoip/GeoIP.dat')
user_country = g.country_code_by_addr(request.remote_addr);
return render_template('home.html', user_country=user_country)