11

I am using python and would like a simple regex to check for a domain name's validity. I check at least write domain name.

url = 'https://stackoverflow'
        keyword = 'foo'
        with self.assertRaises(ValueError):
            check_keyword(url, keyword)

I try unit testing on url textfield and there is main.py page where I done the validation main.py-

def check_keyword(url, keyword):

if re.match("^(((([A-Za-z0-9]+){1,63}\.)|(([A-Za-z0-9]+(\-)+[A-Za-z0-9]+){1,63}\.))+){1,255}$" ,url):
   return ValueError("Invalid")

Example

Vipul Gangwar
  • 149
  • 1
  • 1
  • 10

2 Answers2

17

The source of the validators module shows, that this is maybe a little more complex task.

You could use that module:

>>> import validators
>>> validators.domain('example.com')
True

>>> validators.domain('example.com/')
ValidationFailure(func=domain, ...)

Or you can use the RFCs for Domain names to construct your own checker.

Christian König
  • 3,437
  • 16
  • 28
  • I am using that same module and the regular expression that it uses looks weird. Also, it gives this domain as valid: "a.aa*com" and I think it shouldn't be, specially because it has got an asterisk as a part of it. – user3289695 Sep 28 '17 at 12:52
  • validators.domain("example.com") cannot check for subdomains. So, It wiil return True for validators.domain("abc.example.com") also. – Akash Wankhede May 19 '20 at 06:28
  • The validators package marks domains with [underscores](https://github.com/kvesteri/validators/issues/102) or [trailing dots](https://github.com/kvesteri/validators/issues/141) as invalid. – Nemo Jun 23 '20 at 22:50
0

Try this :

# Check if a string is a url
    from django.core.validators import URLValidator
    import requests

    try:
        validate = URLValidator()
        validate(url)
        print("String is a valid URL")
    except:
        print("String is not valid URL")
        raise serializers.ValidationError("String is not valid URL")
    
    # Check if the url exists on the internet
    try:
        response = requests.get(url)
        print("URL is valid and exists on the internet")
    except requests.ConnectionError as exception:
        print("URL does not exist on Internet")
        raise serializers.ValidationError(f'URL {url} does not exist on Internet')

UPDATE : i found a better solution by using python-whois library here valid domain name by whois

omar ahmed
  • 635
  • 5
  • 19