⎗ ✓ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75import re def validate_url(url): """ Validates a URL with the format: protocol://domain:port/path?security#fragment Returns True if valid, False if invalid, along with any validation messages. Parameters: url (str): The URL to validate Returns: tuple: (bool, list) - (is_valid, list of validation messages) """ messages = [] # Basic URL pattern pattern = r'^(?P<protocol>https?|ftp|file)://' # Protocol pattern += r'(?P<domain>([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\.)*[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)' # Domain pattern += r'(?P<port>:\d{1,5})?' # Optional port pattern += r'(?P<path>/[^?#]*)?' # Optional path pattern += r'(?P<query>\?[^#]*)?' # Optional query pattern += r'(?P<fragment>#.*)?$' # Optional fragment match = re.match(pattern, url) if not match: return False, ["Invalid URL format"] # Extract components components = match.groupdict() # Validate protocol protocol = components['protocol'] if protocol not in ['http', 'https', 'ftp', 'file']: messages.append(f"Invalid protocol: {protocol}") # Validate domain domain = components['domain'] if len(domain) > 255: messages.append("Domain name too long") # Validate port if components['port']: port = int(components['port'][1:]) # Remove the ':' prefix if port < 1 or port > 65535: messages.append(f"Invalid port number: {port}") # Validate path if components['path']: if not all(c.isprintable() for c in components['path']): messages.append("Path contains invalid characters") # Return validation result is_valid = len(messages) == 0 return is_valid, messages # Example usage def test_url_validator(): test_urls = [ "http://example.com:8080/path?query=123#fragment", "https://subdomain.example.com/path", "ftp://invalid:99999/path", "invalid://example.com", "http://example.com:8080/path?security=token#section" ] for url in test_urls: is_valid, messages = validate_url(url) print(f"\nTesting URL: {url}") print(f"Valid: {is_valid}") if messages: print("Messages:", messages) if __name__ == '__main__': test_url_validator() Generated with Claude sonnet 3.5 :)
Warning LINK You are about to visit a link which has been flagged with the above content warnings. Do you wish to continue? Continue Cancel