-
Notifications
You must be signed in to change notification settings - Fork 24
/
util.py
46 lines (42 loc) · 1.29 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import hpfeeds
from logger import LOGGER
import socket
import requests
from requests.exceptions import Timeout, ConnectionError
import logging
logger = logging.getLogger(__name__)
def get_hpfeeds_client(config):
hpc = None
if config['hpfeeds.enabled'].lower() == 'true':
LOGGER.info('hpfeeds enabled, creating connection to {}:{}'.format(config['hpfeeds.host'], config['hpfeeds.port']))
hpc = hpfeeds.new(
config['hpfeeds.host'],
int(config['hpfeeds.port']),
config['hpfeeds.identity'],
config['hpfeeds.secret']
)
hpc.s.settimeout(0.01)
else:
LOGGER.info( 'hpfeeds is disabled')
return hpc
def valid_ip(ip):
try:
socket.inet_aton(ip)
return True
except:
return False
def get_ext_ip(urls):
for url in urls:
try:
req = requests.get(url)
if req.status_code == 200:
data = req.text.strip()
if data is None or not valid_ip(data):
continue
else:
return data
else:
raise ConnectionError
except (Timeout, ConnectionError) as e:
logger.warning('Could not fetch public ip from {0}'.format(url))
return None