Honestly, your solution’s already the simplest for python3 get response code from urllib without adding libs.
But hey, if you’re doing this a lot, wrap it in a function:
```python
def get_status(url):
try:
with urllib.request.urlopen(url) as resp:
return resp.getcode()
except urllib.error.URLError as e:
return f"Error: {e}"
```
Now you can reuse it and pretend urllib isn’t clunky.
But hey, if you’re doing this a lot, wrap it in a function:
```python
def get_status(url):
try:
with urllib.request.urlopen(url) as resp:
return resp.getcode()
except urllib.error.URLError as e:
return f"Error: {e}"
```
Now you can reuse it and pretend urllib isn’t clunky.
