mirror of
https://github.com/Hopiu/django-fobi.git
synced 2026-05-24 04:03:44 +00:00
56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
from django.conf import settings
|
|
|
|
from nine import versions
|
|
|
|
import requests
|
|
|
|
from requests.exceptions import (
|
|
ConnectionError,
|
|
ConnectTimeout,
|
|
ReadTimeout,
|
|
SSLError,
|
|
ProxyError,
|
|
RetryError
|
|
)
|
|
|
|
if versions.DJANGO_LTE_1_11:
|
|
from django.core.urlresolvers import resolve, Resolver404
|
|
else:
|
|
from django.urls import resolve, Resolver404
|
|
|
|
try:
|
|
from localeurl.utils import strip_path
|
|
except ImportError as err:
|
|
strip_path = None
|
|
|
|
__title__ = 'fobi.validators'
|
|
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
|
|
__copyright__ = '2013-2018 Artur Barseghyan'
|
|
__license__ = 'GPL 2.0/LGPL 2.1'
|
|
__all__ = ('url_exists',)
|
|
|
|
|
|
def url_exists(url, local=False):
|
|
"""Check if URL exists.
|
|
|
|
:param str url:
|
|
:param bool local:
|
|
:return bool:
|
|
"""
|
|
if not local:
|
|
try:
|
|
r = requests.head(url)
|
|
return r.status_code == requests.codes.ok
|
|
except (ConnectionError, ConnectTimeout, ReadTimeout, SSLError,
|
|
ProxyError, RetryError) as err:
|
|
return False
|
|
|
|
else:
|
|
if 'localeurl' in settings.INSTALLED_APPS and callable(strip_path):
|
|
url = strip_path(url)[1]
|
|
|
|
try:
|
|
resolve(url)
|
|
return True
|
|
except Resolver404 as err:
|
|
return False
|