EDIT
As @Kwame points out, the code below does validate the URL even if .com or .co etc. are .co .
@Blaise also indicated that URLs such as https://www.google are valid and you need to do a DNS check to see if it allows or not, separately.
It is simple and works:
Thus, min_attr contains the basic set of strings that must be present to determine the validity of the URL, i.e. http:// part and google.com part.
urlparse.scheme stores http:// and
urlparse.netloc stores the google.com domain name
from urlparse import urlparse def url_check(url): min_attr = ('scheme' , 'netloc') try: result = urlparse(url) if all([result.scheme, result.netloc]): return True else: return False except: return False
all() returns true if all the variables inside it return true. Thus, if result.scheme and result.netloc are present, i.e. have some value, then the url is valid and therefore returns true.
Padam Sethia Jul 12 '17 at 6:58 2017-07-12 06:58
source share