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. Find more at httpstatusrappers.com :)
except requests.ConnectionError:
print("failed to connect")
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]
In future, for those that use python3 and later, here's another code to find response code.
import urllib.request
def getResponseCode(url):
conn = urllib.request.urlopen(url)
return conn.getcode()
The urllib2.HTTPError
exception does not contain a getcode()
method. Use the code
attribute instead.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With