Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4001326
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:53:26+00:00 2026-05-20T07:53:26+00:00

I am downloading URLs in Python and need to detect 404s, so after some

  • 0

I am downloading URLs in Python and need to detect 404s, so after some search I came up with:

import urllib
class MyUrlOpener(urllib.FancyURLopener):
    def retrieve(self, url, filename=None, reporthook=None, data=None):
        self.file_was_found = True
        val = urllib.FancyURLopener.retrieve(self, url, filename, reporthook, data)        
        return val

    def http_error_404(url, fp, errcode, errmsg, headers, data):
        url.file_was_found = False


def download_file(url, saveas):
    urlaccess = MyUrlOpener()
    localFile, headers = urlaccess.retrieve(url, saveas)
    return urlaccess.file_was_found

My question is that if you look at the source code (Python 2.7) for FancyURLopener then you see:

def http_error(self, url, fp, errcode, errmsg, headers, data=None):
    """Handle http errors.
    Derived class can override this, or provide specific handlers
    named http_error_DDD where DDD is the 3-digit error code."""
    # First check if there's a specific handler for this error
    name = 'http_error_%d' % errcode
    if hasattr(self, name):
        method = getattr(self, name)
        if data is None:
            result = method(url, fp, errcode, errmsg, headers)
        else:
            result = method(url, fp, errcode, errmsg, headers, data)
        if result: return result
    return self.http_error_default(url, fp, errcode, errmsg, headers)

Which is passing the url as the first parameter and not self. I thought that the first parameter to a function was always a reference to the class instance (by convention) and my code confirms this. So what happens to the url value?

UPDATE: It turns out that data==None so it was calling the first signature. This foiled my attempts to manually add the self parameter. As soon as I added the =None default to data in my http_error_404 signature all was well (because it used the default).

The fixed / correct signature is def http_error_404(self, url, fp, errcode, errmsg, headers, data=None):

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:53:27+00:00Added an answer on May 20, 2026 at 7:53 am

    In Python, any class instance’s method has self passed in by the Python interpreter and all of the other arguments are shifted down one place automatically.

    In other words the Python interpreter rewrites:

    urlaccess.retrieve(url, saveas)
    

    into something that looks like this:

    urlaccess.retrieve(urlaccess, url, saveas)
    

    So you don’t have to do it yourself. However, since

    explicit is better than implicit

    any instance methods you declare for a Python object must specify explicitly that they take the instance of the object as their first argument even though Python will pass that argument without any action on the part of the programmer.

    The first argument does not have to be called self … that is only a convention.


    So, to actually answer your question though (as mluebke did) — you need to specify the self argument.

    def http_error_404(url, fp, errcode, errmsg, headers, data):
        url.file_was_found = False
        # Python is treating `url` as `self`
        # Therefore the URL is being saved in `fp`, `fp` in `errcode`, etc.
    

    To fix this problem add a first argument to pick up the instance.

    def http_error_404(self, url, fp, errcode, errmsg, headers, data):
        self.file_was_found = False
        # Now everything should work
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After downloading all .NET framework symbols and sources using NetMassDownloader , is it possible
After downloading files from a remote UNIX FTP server, you want to verify that
I'm downloading some images from a service that doesn't always include a content-type and
I have an application that downloading urls using threadPool in different threads, but recently
I am downloading a text-file from example.com/test.txt and I need the contents only during
I need to allow multiple downloading of small documents in Rails, preferably using Paperclip
I have a list of around 3000 image URLs, where I need to download
Downloading the Ninject.MVC3 package from NuGet creates this App_Start folder with a simple class
I'm downloading a ~3MB text file using WebClient.DownloadString() , but I only need the
I'm downloading a web page (tag soup HTML) with XMLHttpRequest and I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.