How to implement curl -u in Python?

If it's 404, you probably just have the wrong URL. If it's 403, perhaps you have the realm wrong.

For starters, you're passing the URL to add_password, when in fact you should only be passing the base URL. Also, instead of install_opener, you should probably just create a new opener.

See this recipe for an example:

class NoOpHandler(urllib2.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, headers, newUrl):
        return None

passmanager = urllib2.HTTPPasswordMgrWithDefaultRealm()
passmanager.add_password(None, baseurl, username, password)
auth_handler = urllib2.HTTPBasicAuthHandler(passmanager)
opener = urllib2.build_opener(auth_handler, NoOpHandler())

r = requests.get('https://api.github.com', auth=('user', 'pass'))

Python requests is the way to go here. I've been using requests extensively at work and at home for various web service interactions. It is a joy to use compared to what came before it. Note: the auth keyword arg works on any call that requires auth. Thus, you can use it sparingly, i.e. you don't need it for every call against GitHub, only those that require logins. For instance:

r = requests.get('https://api.github.com/gists/starred', auth=('user', 'pass'))

The GitHub login is documented here:

http://pypi.python.org/pypi/requests/0.6.1