Python Forum

Full Version: Response [200] little help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,
Here is my code.
import requests

r = requests.get("http://www.noobmovies.com")
if r == 200:
    print("Website is up and running")

else:
    print("Web site is down")

    
print(r)
What the code should do is if I get a status code back of 200 it print out "Website is up and running", other than a 200 it prints "Web site is down"

The code does not do that. here is what I get

Web site is down
<Response [200]>
I don't know if the 200 is an int or a string. That could be the problem.
I will wait untill someone might know what is wrong with my code.
Thank You
renny Doh
It looks like r is a Response object. I believe you want to test r.status_code == 200.
Thank you for the Doc. Developer Interface Response, that going help a lot. the code works.
renny
Here's a list of all status codes: https://www.w3.org/Protocols/HTTP/HTRESP.html
Oh, that is great, just what I needed
I just took another look at that document. It's not up to date, but has a link to the latest codes.
This looks like a more up to date list: http://www.tcpipguide.com/free/t_HTTPSta...ases-3.htm

And the official list: http://www.iana.org/assignments/http-sta...odes.xhtml
Improved version. Using a function to make the code reusable.
You should also look for a connection error, because when the destination is not available,
you don't get a result with your code.

Never use try: except: without specifying which error you want to catch.

import requests
from requests.exceptions import ConnectionError

def is_up(url):
   try:
       r = requests.get(url)
   except ConnectionError:
       return False
   if r.status_code == 200:
       return True
   else:
       return False

urls = ['http://www.noobmovies.com', 'http://www.noobmovies.cxy']

for url in urls:
   print('url:', url, is_up(url))
If you want the status code, it's a small change of the function. Then you may return for example with None, if there is a connection error.
Thank you DeaD_eye, That works very nice.
(Jun-25-2017, 09:18 PM)DeaD_EyE Wrote: [ -> ]
def is_up(url):
   try:
       r = requests.get(url)
   except ConnectionError:
       return False
   if r.status_code == 200:
       return True
   else:
       return False

Improving the improved version! Because extra blocks look like spaghetti to me.
def is_up(url):
  try:
      r = requests.get(url)
      return r.status_code == 200
  except ConnectionError:
      return False
(Jul-11-2017, 06:13 PM)nilamo Wrote: [ -> ]Improving the improved version!  Because extra blocks look like spaghetti to me.
def is_up(url):
  try:
      r = requests.get(url)
      return r.status_code == 200
  except ConnectionError:
      return False

Improving the improved improved version!

def is_up(url):
    try:
        return requests.get(url).status_code == 200
    except ConnectionError:
        return False
Dance
Pages: 1 2