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!
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!
