私のddnsホスト名にpingを実行し、ホストがアップまたはダウンしている場合は、次のコードを返します。私は通常のホスト名www.iamsimonsmale.co.uk
では動作しますが、http://ssmale.ddns.net
では動作しないsocket.gethostbyname
を追加しようとしました。socket.gethostbynameが自分のddnsホスト名で動作しない
アドレスの先頭から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
です
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?
、それが動作し、印刷が
あるwww.iamsimonsmale.co.uk `で行われています86.136.251.202
Server Up
私はまた、How do I get the IP address from a http request using the requests library?のコードを成功裏に使用しようとしました。このツール(http://mxtoolbox.com/SuperTool.aspx#)を使用して
私は問題の原因とどのように私はそれを修正するだろうされて何ssmale.ddns.net
ためのDNSのレコードがあることを確認しましたか? http://
部分のないアドレスの
今それを見ると明らかです。私は両方を使うことについて考えなかった。助けてくれてありがとう – TheStudent
@TheStudentそれはいつもより明らかです。お役に立てて嬉しいです。 – margold