-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
50 lines (40 loc) · 1.31 KB
/
utils.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
47
48
49
50
import socket
import re
import datetime
import dateutil.parser
import pytz
def check_reachability_server(endpoint):
"""
Check whether the enpoint server is reachable
:param endpoint: endpoint url
:return: True if enpoint is reachable, False otherwise
"""
m = re.match(r'^((http|https):\/\/)?([^\/]*).*$', endpoint)
port = 80
if m.group(2) == "https":
port = 443
hostname = m.group(3)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(0.1)
try:
sock.connect((hostname, port))
except socket.error:
return False
finally:
sock.close()
return True
def convert_date(date_string):
"""
Convert a date string to datetime (if not already datetime) and attach timezone (if not already attached)
:param date_string: Date string to convert
:return: datetime with correct timezone attached
"""
if isinstance(date_string, str):
date = dateutil.parser.parse(date_string)
elif isinstance(date_string, datetime.datetime):
date = date_string
else:
raise ValueError("Unsupported date type: " + type(date_string))
if date.tzinfo is None: # localize time if necessary
date = pytz.timezone('Europe/Zurich').localize(date)
return date