-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathurl_func.py
62 lines (50 loc) · 1.63 KB
/
url_func.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
51
52
53
54
55
56
57
58
59
60
61
62
import requests
import validators
# import socket
import whois
from tld import get_tld
# import time
CONNECTION_OK = "connected to internet"
CONNECTION_NA = "not connected to internet"
# def check_connection2(url='www.google.com/'):
# # noinspection PyBroadException
# try:
# # see if we can resolve the host name -- tells us if there is
# # a DNS listening
# host = socket.gethostbyname(url)
# # connect to the host -- tells us if the host is actually
# # reachable
# socket.create_connection((host, 80), 2)
# return CONNECTION_OK
# except:
# pass
# return CONNECTION_NA
def check_connection(url='http://www.google.com/', timeout=5):
try:
req = requests.get(url, timeout=timeout)
# HTTP errors are not raised by default, this statement does that
req.raise_for_status()
# logger.info('-> Connection Status : '+CONNECTION_OK)
return CONNECTION_OK
except requests.HTTPError as e:
rs = "Http Error, status code {0}.".format(e.response.status_code)
# logger.info('-> Connection Status : '+rs)
return rs
except requests.ConnectionError:
# logger.info('-> Connection Status : '+CONNECTION_NA)
return CONNECTION_NA
except requests.RequestException:
return None
def check_url_syntax(url):
rv = True
# noinspection PyBroadException
try:
if not validators.url(str(url)):
rv = False
except:
rv = False
return rv
def return_tld(url):
return get_tld(url, fail_silently=True)
def get_domain_info(domain):
return whois.whois(domain)