Created
February 10, 2015 14:36
-
-
Save embayer/a49f7303ae3e4f6a441c to your computer and use it in GitHub Desktop.
simple url validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def validate_url(url): | |
| """ | |
| validates a homepage | |
| string url: the url to validate | |
| string return: the validate url or "" | |
| """ | |
| regex = re.compile( | |
| r'^((?:http|http|ftp)s?://|www\.)' # http:// or https:// or ftp | |
| r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain... | |
| r'localhost|' #localhost... | |
| r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip | |
| r'(?::\d+)?' # optional port | |
| r'(?:/?|[/?]\S+)$', re.IGNORECASE) | |
| match = re.search(regex, url) | |
| if match: | |
| return url | |
| else: | |
| return "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment