What’s the best way to get an HTTP response code from a URL?
python
Solution
Update using the wonderful requests library. Note we are using the HEAD request, which should happen more quickly then a full GET or POST request.
import requests
try:
r = requests.head("https://stackoverflow.com")
print(r.status_code)
# prints the int of the status code*
except requests.ConnectionError:
print("failed to connect")
*Find more at https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
Problem
I’m looking for a quick way to get an HTTP response code from a URL (i.e. 200, 404, etc). I’m not sure which library to use.