Python Forum
Response [200] little help
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Response [200] little help
#1
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
Reply
#2
It looks like r is a Response object. I believe you want to test r.status_code == 200.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you for the Doc. Developer Interface Response, that going help a lot. the code works.
renny
Reply
#4
Here's a list of all status codes: https://www.w3.org/Protocols/HTTP/HTRESP.html
Reply
#5
Oh, that is great, just what I needed
Reply
#6
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
Reply
#7
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
Thank you DeaD_eye, That works very nice.
Reply
#9
(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
Reply
#10
(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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

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