如何使用Python验证域名是否符合RFC 1035? [英] How can I validate that a domain name conforms to RFC 1035 using Python?

查看:303
本文介绍了如何使用Python验证域名是否符合RFC 1035?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些将采用假定的"域名并根据RFC 1035对其进行验证的代码.例如,它需要满足以下规则:

I'm trying to write some code that will take in a "supposed" domain name and will validate it according to RFC 1035. For instance, it would need to satisfy these rules:

  • 域最多包含253个字符
  • 域字符集仅是[a-z0-9\-](将小写输入域)
  • 域不能包含两个连续的破折号(例如:google--com.com)
  • 最大子域限制为127
  • Domain consists of no more than 253 total characters
  • Domain character set is [a-z0-9\-] only (will lower case the domain on input)
  • Domain cannot contain two consecutive dashes (eg: google--com.com)
  • There is a maximum subdomain limit of 127

我搜索了各种Python模块(例如tldextract),但无济于事.

I have searched for various Python modules (eg: tldextract) but to no avail.

如何验证域名符合RFC 1035?

How can I validate that a domain name conforms to RFC 1035?

推荐答案

吻:

import string

VALID_CHARS = string.lowercase + string.digits + '-.'

def is_valid_domain(domain):
    if not all(char in VALID_CHARS for char in domain.lower()):
        return False
    if len(domain) > 253:
        return False
    if '--' in domain:
        return False
    if '..' in domain:
        return False
    return True

有时会有聪明,但这似乎并不是其中之一.

There are times for cleverness, but this doesn't seem to be one of them.

这篇关于如何使用Python验证域名是否符合RFC 1035?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆