Python Forum
How can I check out my localhost? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How can I check out my localhost? (/thread-4280.html)



How can I check out my localhost? - Mike Ru - Aug-04-2017

I am using a libre requests. I am trying to check out localhost and if it is working then to type the message "Yes". If it isn't working then to type the message "No". Now my localhost is working and after I run my script I get the message "Yest" and it is normally. But when my localhost isn't working I don't get the message "No" . I only get a stack of errors.
Here is my code:


import requests

url = 'http://localhost:8000'
response = requests.get(url)
if response.status_code == 200:
    print("Yes ...")
else:
    print("No ...")
Why doesn't it type the message "No" when the localhost isn't working ?


RE: How can I check out my localhost? - buran - Aug-05-2017

because your localhost is not working. i.e. you don't get any response (and its status code), but  errors
import requests

url = 'http://localhost:8000'
try:
    response = requests.get(url)
    if response.status_code == 200:
        print("Everything is OK. Status code 200 ...")
    else:
        print("Localhost OK, but get status code {} ...".format(resposne.status_code))
except requests.ConnectionError:
    print("Localhost not found")



RE: How can I check out my localhost? - Mike Ru - Aug-05-2017

(Aug-05-2017, 05:21 AM)buran Wrote: because your localhost is not working. i.e. you don't get any response (and its status code), but  errors
Super. Thank you so much!