arjun9916
arjun9916

Reputation: 91

To print http status code in Python 3 (urllib)

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?

Upvotes: 3

Views: 13866

Answers (3)

SAGAR BHOOSHAN
SAGAR BHOOSHAN

Reputation: 329

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)}')

Upvotes: 5

Agnes Kis
Agnes Kis

Reputation: 499

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

Upvotes: 4

Moses Koledoye
Moses Koledoye

Reputation: 78556

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))

Upvotes: 8

Related Questions