0

What I'm trying to do

I'm requesting a file from an API. If the file does't exist, I get a 404.

What I tried

I'm trying to handle this, using urllib3.

I found a lot of great, but outdated (~10 years old), documentation how to do this with with urllib and urllib2.

How does this work in urllib3?

All I found in their docs was this

try:
    http.request('GET', 'nx.example.com', retries=False)
except urllib3.exceptions.NewConnectionError:
    print('Connection failed.')
blkpingu
  • 1,556
  • 1
  • 18
  • 41

1 Answers1

0

You can simply look at the status code:

import urllib3

http = urllib3.PoolManager()
r = http.request("GET", "httpbin.org/status/404")
if r.status == 404:
    print("404!")
Quentin Pradet
  • 4,691
  • 2
  • 29
  • 41