2016-06-01 6 views
0

私の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://部分のないアドレスの

答えて

0

問合せ:

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

そして、あなたのリクエストクエリで完全なアドレスを使用します。

>>> import socket 

>>> socket.gethostbyname("ssmale.ddns.net") 
'86.136.251.202' 

>>> import requests 

>>> requests.get("http://ssmale.ddns.net") 
<Response [200]> 

:私のマシンで

response = requests.get("http://ssmale.ddns.net") 

作品あなたが投稿したエラーメッセージには既にそれが記載されています:No schema supplied. Perhaps you meant http://ssmale.ddns.net?

+0

今それを見ると明らかです。私は両方を使うことについて考えなかった。助けてくれてありがとう – TheStudent

+0

@TheStudentそれはいつもより明らかです。お役に立てて嬉しいです。 – margold

関連する問題