socket.gethostbyname不适用于我的ddns主机名

时间:2016-06-01 10:05:24

标签: python sockets dns ping

我有以下代码来ping我的ddns主机名,并在主机启动或关闭时返回。然后我尝试添加适用于我的普通主机名socket.gethostbyname但不适用www.iamsimonsmale.co.uk的{​​{1}}。

我试图从地址的开头删除http://ssmale.ddns.net,但这也失败了。

http://

#!/usr/bin/env python # This script checks to see if the server is up or down and prints the pinged IP import requests import socket import time while True: host = 'http://ssmale.ddns.net' ip = socket.gethostbyname(host) print ip response = requests.get(host) if response.status_code == requests.codes.ok: print('Server Up') else: print('Server Down') time.sleep(10) 的错误消息是

http://ssmale.ddns.net

Traceback (most recent call last): File "PingTestIP.py", line 12, in <module> ip = socket.gethostbyname('http://ssmale.ddns.net') socket.gaierror: [Errno -2] Name or service not known

ssmale.ddns.net

当完成`www.iamsimonsmale.co.uk时,它的工作原理和印刷品是

Traceback (most recent call last):
File "PingTestIP.py", line 16, in <module>
response = requests.get(host)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 60, in get
return request('get', url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 443, in request
prep = self.prepare_request(req)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 374, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/lib/python2.7/dist-packages/requests/models.py", line 304, in prepare
self.prepare_url(url, params)
File "/usr/lib/python2.7/dist-packages/requests/models.py", line 358, in prepare_url
"Perhaps you meant http://{0}?".format(url))
requests.exceptions.MissingSchema: Invalid URL u'ssmale.ddns.net': No schema supplied. Perhaps you meant http://ssmale.ddns.net?

我还试图使用How do I get the IP address from a http request using the requests library?中的代码但没有成功。

使用此工具(http://mxtoolbox.com/SuperTool.aspx#)我确认DNS中有ssmale.ddns.net的A记录

导致问题的原因是什么以及如何解决?

1 个答案:

答案 0 :(得分:0)

查询没有ddlCarrier.Text部分的地址:

pip3 install numpy

然后在请求查询中使用完整地址:

http://

在我的机器上运行:

ip = socket.gethostbyname('ssmale.ddns.net')

您发布的错误消息已说明:response = requests.get("http://ssmale.ddns.net")