您可以
socket.gethostbyname()为此使用:
>>> import socket>>> socket.gethostbyname('google.com')'74.125.224.198'>>> socket.gethostbyname('foo')# no host 'foo' exists on the networkTraceback (most recent call last): File "<stdin>", line 1, in <module>socket.gaierror: [Errno 8] nodename nor servname provided, or not known
您的函数可能如下所示:
def hostname_resolves(hostname): try: socket.gethostbyname(hostname) return 1 except socket.error: return 0
例:
>>> hostname_resolves('google.com')1>>> hostname_resolves('foo')0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)