Skip to content

Instantly share code, notes, and snippets.

@jaydeepkarale
Last active June 24, 2022 16:25
Show Gist options
  • Save jaydeepkarale/be2168fda3a563d0033af8c6840a77eb to your computer and use it in GitHub Desktop.
Save jaydeepkarale/be2168fda3a563d0033af8c6840a77eb to your computer and use it in GitHub Desktop.
Validate URL Snippets
from aiohttp import web
from http import HTTPStatus
import validators
import requests
def validate_url_format(longurl: str):
"""This function returns True if the longurl is in a valid format"""
return validators.url(longurl)
def validate_url(longurl: str):
"""This function retruns True if the longurl is a valid web address"""
try:
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'}
response = requests.get(longurl, headers=headers)
return response.status_code == HTTPStatus.OK
except Exception as ex:
print(ex)
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment