Domain check with Python

In one project, I had to check if the passed string was a domain, so I dealt with it there.

import socket
domain = "qiita.com"

class DomainError(BaseException): pass

def check_domain(domain):
    if domain.find(".") != -1:
            try:
                host = socket.gethostbyname(domain)
                return True

            except socket.gaierror:
                raise DomainError("domain not found.")
    else:
        raise DomainError("this is not domain.")

First, check if the domain string contains dots, and then use the socket.gethostbyname function of the ** socket ** module to convert the ** domain string to an IP address. ** If this works, it returns True because the given domain string is a normal domain. If the function gives an error, it's not a normal domain and throws an error.

Recommended Posts

Domain check with Python
Check version with python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Play with 2016-Python
Tested with Python
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
[Python] Easy argument type check with dataclass
Check the existence of the file with python
Check stock prices with slackbot using python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Read data with python / netCDF> nc.variables [] / Check data size
Learn Python with ChemTHEATER
Automatically check Python scripts with GitHub + Travis-CI + pycodestyle
1.1 Getting Started with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Check the date of the flag duty with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Run Blender with python
Solve Sudoku with Python