Python Forum
How to check HTTP error 500 and bypass
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check HTTP error 500 and bypass
#1
Hi

I want to catch dara from web, but some the web reload for ever (without any error), and some time it catch firewall isuse(HTTP error 500).

But below code, when HTTP error 500, it still read data using, but I want to bypass it. I only want to read data when no error or exceptions.

from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
import requests

webinput='https://www.google.com/'

try:
    f=requests.get(webinput,timeout=10).text
    if len(f>0):
        print('Web connection is fine')
        a=
    else:
        print('Web server issue')
except HTTPError as e:
    print('Error code:', e.code)
except URLError as e:
    print('We fail to reach server')
    print('Reason:', e.reason)
Reply
#2
No need to use urllib Exception,requests has own Errors and Exceptions.
Quote:All exceptions that Requests explicitly raises inherit from requests.exceptions.RequestException.
So a catch all error scenario could be.
import requests
from requests.exceptions import RequestException

url1 = "http://www.not_exist.com/"
url2 = "https://httpbin.org/status/500"
url3 = "https://httpbin.org/delay/15"
url4 = 'https://www.google.com/' # ok
try:
    response = requests.get(url4, timeout=10)
    response.raise_for_status()
    print(response.status_code) # All ok 200
except RequestException as error:
    print(error)
Reply
#3
I want to bypass all exception, and read data only when All ok 200.
Reply
#4
Can pass out all,and only do a check for 200.
import requests
from requests.exceptions import RequestException

url1 = "http://www.not_exist.com/"
url2 = "https://httpbin.org/status/500"
url3 = "https://httpbin.org/delay/15"
url4 = 'https://www.google.com/' # ok
try:
    response = requests.get(url4, timeout=10)
    if response.status_code == 200:
        print(response.status_code) # All ok 200
except RequestException:
    pass
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTTP 404 error with Session Pool Clives 0 242 Jun-17-2021, 06:45 PM
Last Post: Clives
  error HTTP Error 403: Forbidden local_bit 1 1,122 Nov-14-2020, 11:34 AM
Last Post: ndc85430
  urllib.error.HTTPError: HTTP Error 404: Not Found ckkkkk 4 4,283 Mar-03-2020, 11:30 AM
Last Post: snippsat
  python beginner HTTP Error 500 leofcastro 0 783 Jan-24-2020, 04:37 PM
Last Post: leofcastro
  HTTP error 404 Karin 4 2,716 May-31-2019, 02:23 PM
Last Post: snippsat
  selneium JS bypass metulburr 15 3,262 Nov-05-2018, 10:52 AM
Last Post: Larz60+
  Syntax error for HTTP request GET THX1138 1 4,505 May-12-2018, 12:02 PM
Last Post: snippsat
  selenium bypass javascript popup box metulburr 6 5,354 Jun-02-2017, 07:15 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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