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.
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.
Here's a solution that uses httplib instead.
import httplib
def get_status_code(host, path="/"):
""" This function retreives the status code of a website by requesting
HEAD data from the host. This means that it only requests the headers.
If the host cannot be reached or something else goes wrong, it returns
None instead.
"""
try:
conn = httplib.HTTPConnection(host)
conn.request("HEAD", path)
return conn.getresponse().status
except StandardError:
return None
print get_status_code("stackoverflow.com") # prints 200
print get_status_code("stackoverflow.com", "/nonexistant") # prints 404
You should use urllib2, like this:
import urllib2
for url in ["http://entrian.com/", "http://entrian.com/does-not-exist/"]:
try:
connection = urllib2.urlopen(url)
print connection.getcode()
connection.close()
except urllib2.HTTPError, e:
print e.getcode()
# Prints:
# 200 [from the try block]
# 404 [from the except block]
Addressing @Niklas R's comment to @nickanor's answer:
from urllib.error import HTTPError
import urllib.request
def getResponseCode(url):
try:
conn = urllib.request.urlopen(url)
return conn.getcode()
except HTTPError as e:
return e.code
It depends on multiple factories, but try to test these methods:
import requests
def url_code_status(url):
try:
response = requests.head(url, allow_redirects=False)
return response.status_code
except Exception as e:
print(f'[ERROR]: {e}')
or:
import http.client as httplib
import urllib.parse
def url_code_status(url):
try:
protocol, host, path, query, fragment = urllib.parse.urlsplit(url)
if protocol == "http":
conntype = httplib.HTTPConnection
elif protocol == "https":
conntype = httplib.HTTPSConnection
else:
raise ValueError("unsupported protocol: " + protocol)
conn = conntype(host)
conn.request("HEAD", path)
resp = conn.getresponse()
conn.close()
return resp.status
except Exception as e:
print(f'[ERROR]: {e}')
Benchmark results for 100 URLs: