检查字符串中的有效域名? [英] Check for valid domain name in a string?

查看:64
本文介绍了检查字符串中的有效域名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python,并希望使用一个简单的正则表达式来检查域名的有效性.我至少检查写域名.

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)

我尝试对url文本字段进行单元测试,并且在main.py页面中进行了验证main.py-

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

def check_keyword(网址,关键字):

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")

示例

  • www.google(无效)

  • www.google (Invalid)

https://stackoverflow (无效)

推荐答案

validators模块的noreferrer>源显示,这可能是一个稍微复杂一点的任务.

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

您可以使用该模块:

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

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

或者您可以将RFC用于域名,以构造自己的检查器.

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

这篇关于检查字符串中的有效域名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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