How to Validate a Domain Name in Python

Domain validation, while useful, is a difficult feature to create from scratch. We will be simplifying this process immensely by applying an easy-to-use API to the problem at hand.

Install the client we need first.

pip install cloudmersive-validate-api-client

Now we are able to call the domain_check function, which takes our domain name as an argument.

from __future__ import print_functionimport timeimport cloudmersive_validate_api_clientfrom cloudmersive_validate_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_validate_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_validate_api_client.DomainApi(cloudmersive_validate_api_client.ApiClient(configuration))domain = 'domain_example' # str | Domain name to check, for example \"cloudmersive.com\".  The input is a string so be sure to enclose it in double-quotes.try:# Validate a domain nameapi_response = api_instance.domain_check(domain)pprint(api_response)except ApiException as e:print("Exception when calling DomainApi->domain_check: %s\n" % e)

OK, you’re done! No more problems with invalid domains!

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store