URL地址常见协议主要包含http/https和ftp三种。验证URL地址有效性首要步骤是检查网址头部是否包含协议。如果必须包含显式的协议声明,则提取协议并判断是否在常见协议列表中。
URL地址协议格式一般为:
[schema]://[dns];
或[schema]://[dns]/[path],path in ([directory] , [path]/[directory]);
schema in ('http', 'https', 'ftp')。
URL网址中的域名DNS至少包含一个域名分割符号 -- “."。该分割符只能在域名的中间任意位置,不包含首位和末尾的位置。
"""
@author: MR.N
@created: 2021/07/19 Mon. 20:27
@version: 1.0
"""
def verify_prefix(url=None):
if url is None or len(url.strip()) < 7:
return False
if not url.startswith('http://') and not url.startswith('https://') and not url.startswith('ftp://'):
return False
return True
def verify_path_loose(url=None):
if not verify_prefix(url):
return False
path = url[url.index('://')+3:]
try:
if not path.index('.') > 0 or not path.index('.') < len(path) - 1:
return False
except ValueError:
return False
return True

本文介绍了URL地址的常见协议,如http、https和ftp,并详细阐述了验证URL有效性的方法,包括检查协议头部和域名DNS的正确性。提供的两个函数`verify_prefix`和`verify_path_loose`分别用于验证URL前缀和路径的合法性。对于URL路径,要求至少包含一个域名分割符号.且不位于首位或末尾。

9471

被折叠的 条评论
为什么被折叠?



