def block_http(whitelist):
def whitelisted(self, host, *args, **kwargs):
try:
string_type = basestring
except NameError:
# python3
string_type = str
if isinstance(host, string_type) and host not in whitelist:
logger.warning('Denied HTTP connection to: %s' % host)
raise MockHttpCall(host)
logger.debug('Allowed HTTP connection to: %s' % host)
return self.old(host, *args, **kwargs)
whitelisted.blockade = True
if not getattr(httplib.HTTPConnection, 'blockade', False):
logger.debug('Monkey patching httplib')
httplib.HTTPConnection.old = httplib.HTTPConnection.__init__
httplib.HTTPConnection.__init__ = whitelisted
评论列表
文章目录