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

16 Replies, 889 Views

urllib’s design is just... old school. For python3 get response code from urllib, you’re stuck with `getcode()` or digging into exceptions. Annoying, but that’s how it is.

If you’re just checking URLs, maybe use `curl` in a subprocess?

```python
import subprocess
result = subprocess.run(['curl', '-s', '-o', '/dev/null', '-w', '%{http_code}', 'https://example.com'], capture_output=True, text=True)
print(result.stdout)
```

Overkill? Maybe. Works? Absolutely.

Messages In This Thread



Users browsing this thread: 1 Guest(s)