Home  >  Article  >  Backend Development  >  How to Verify the Legitimacy of an IP Address in Python?

How to Verify the Legitimacy of an IP Address in Python?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-22 19:24:18664browse

How to Verify the Legitimacy of an IP Address in Python?

How to ascertain the Validity of an IP Address in Python

Question:

How can you effectively validate if an IP address entered by a user is legitimate? This user input is provided as a string.

Answer:

Abstain from manually analyzing the address. Instead, utilize Python's inbuilt socket module's inet_aton function to conduct the validation.

import socket

try:
    socket.inet_aton(addr)
    # Legal
except socket.error:
    # Illegal

The above is the detailed content of How to Verify the Legitimacy of an IP Address in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn