gpt4 book ai didi

Python - 对于 url,如何检查网络服务器是否可用或存在? (使困惑)

转载 作者:行者123 更新时间:2023-12-04 17:07:02 24 4
gpt4 key购买 nike

对于用户输入的url:

check that is any webserver hosting the domain name or not ?

例如:

url='laksjdfaksdfjajdfaljewoifjadslkjflkasjrlwkejk.com'

Now, the above url does-not exists, so there is no point of check for any HTTP error codes since, no server is there that will reply back.

那么,如何检测这样的 url 呢?

isServerUp() ?

最佳答案

这应该会为您打开 url;显然改变了你想说的 url 未能打开的方式。文档是:http://docs.python.org/library/urllib2.html

from urllib2 import *

req = Request(url)

# Try to open the url
try:
reponse = urlopen(req)
except HTTPError, e:
url = None
except URLError, e:
url = None

编辑:固定缩进

关于Python - 对于 url,如何检查网络服务器是否可用或存在? (使困惑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7159631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com