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

16 Replies, 880 Views

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.

Messages In This Thread



Users browsing this thread: 1 Guest(s)