Python Forum
How can I check out my localhost?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I check out my localhost?
#1
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 ?
Reply
#2
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")
Reply
#3
(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!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020