How to Python3 get response code from urllib? Best way to check HTTP status? or Python3 get response

16 Replies, 939 Views

Python3 get response code from urllib – am I missing something?

Hey folks,

Trying to python3 get response code from urllib but keep getting weird errors or nothing at all. Like, I just wanna check if a URL is alive, ya know?

Using `urllib.request.urlopen()` but the status code feels hidden. Do I really need to dig into `getcode()` or catch exceptions just for this?

```python
import urllib.request
try:
with urllib.request.urlopen('https://example.com') as response:
print(response.getcode()) # finally found it!
except urllib.error.URLError as e:
print("RIP, site's down:", e)
```

Is this the simplest way or am I overcomplicating it?

Also, why doesn’t it just *give* me the code upfront? Feels like extra steps.

Thx in advance!

Messages In This Thread
How to Python3 get response code from urllib? Best way to check HTTP status? or Python3 get response - by - 06-09-2024, 01:03 AM



Users browsing this thread: 1 Guest(s)