2

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?

zondo
  • 19,901
  • 8
  • 44
  • 83
arjun9916
  • 71
  • 1
  • 4
  • 8
  • is your real question: how to disable redirects? (so that `urlopen()` won't follow any 30x redirects automatically?) – jfs Jun 12 '16 at 23:24
  • Yes, I do not want the url to be redirected. Just to print the response codes along with response time. – arjun9916 Jun 14 '16 at 20:42
  • see [Is there an easy way to request a URL in python and NOT follow redirects?](http://stackoverflow.com/q/110498/4279) – jfs Jun 14 '16 at 21:44

3 Answers3

7

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))
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
5

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)}')
SAGAR BHOOSHAN
  • 309
  • 2
  • 11
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
Agnes Kis
  • 491
  • 3
  • 6
  • is the requests from > [here](https://2.python-requests.org/en/v2.7.0/user/install/) using `pip install requests`? IF so you should have mentioned that in your answer. – ZF007 Jun 23 '19 at 21:57