To print http status code in Python 3 (urllib)

10,173

Solution 1

Not all errors of class URLError will have a code, some will only have a reason.

Besides, catching URLError and HTTPError in the same except block is not a good idea (see docs):

def getUrl(urls):
   for url in urls:
        try:
           with urllib.request.urlopen(url) as response:
                log(fh, response.code, url)
        except urllib.error.HTTPError  as e:
            log(fh, e.code, url)
        except urllib.error.URLError as e:
            if hasattr(e, 'reason'):
                log(fh, e.reason, url)
            elif hasattr(e, 'code'):
                log(fh, e.code, url)

 def log(fh, item, url):
     print(item)
     fh.write("%s, %s\n" % (item, url))

Solution 2

Instantiate the following in your code.

   try:
        response = urllib.request.urlopen(url)
        code = response.getcode()
        print(code)

    except Exception as e:
        print(f'Error:  {url} : {str(e)}')

Solution 3

Try python's requests package. (docs here)

Bit more straightforward and very easy to print http status codes.

import requests

URL = 'http://yourURL:8080'
query = {'your': 'dictionary to send'}

response = requests.post(URL, data=query)

return response.status_code
Share:
10,173
arjun9916
Author by

arjun9916

Updated on June 05, 2022

Comments

  • arjun9916
    arjun9916 almost 2 years

    I'm trying to get http status codes including 3XX, but from my code I'm not able to print it.

    Here is the code:

    import urllib
    import urllib.request
    import urllib.error
    
    urls = ['http://hotdot.pro/en/404/', 'http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org', 'http://www.voidspace.org.uk']
    fh = open("example.txt", "a")
    def getUrl(urls):
       for url in urls:
            try:
               with urllib.request.urlopen(url) as response:
                    requrl = url
                    the_page = response.code
                    fh.write("%d, %s\n" % (int(the_page), str(requrl)))
            except (urllib.error.HTTPError, urllib.error.URLError)  as e:
                requrl = url
                print (e.code)
                fh.write("%d, %s\n" % (int(e.code), str(requrl)))
    getUrl(urls)
    

    Can someone help me with this?