pythonteamcity-8.0

how can I use python 3 to download a Teamcity artifact with basic auth


I'm trying to download a zip artifact from teamcity using python 3 and i'm not having much luck.

From the browser I would normally do this: http://USERNAME:PWD@SERVER/httpAuth/repository/downloadAll/dood_dad/latest.lastSuccessful

But if I try this with urllib.request.urlretrieve I get an exception about an invalid port - because It doesn't know about the username and password appended to the front of the url and is parsing after the ':' as a port - fair enough.

So I guess I need to use teamcitys httpAuth stuff and use the url http://SERVERNAME/httpAuth/repository/downloadAll/dood_dad/latest.lastSuccessful

When I try that I get a 404 Unauthorized which I expected because I need to supply the username and password.

But I can't work out how.

I've added this:

auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(None,
                          uri=url_to_open,
                          user='userame',
                          passwd='password')
opener = urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)
local_filename, headers = urllib.request.urlretrieve(url)

But I'm still getting HTTP Error 401: Unauthorized

TIA.


Solution

  • this works ok:

    import urllib
    from urllib.request import HTTPPasswordMgrWithDefaultRealm
    
    pwdmgr = HTTPPasswordMgrWithDefaultRealm()
    pwdmgr.add_password(None, uri=url, user='XXXX', passwd='XXXX')
    auth_handler = urllib.request.HTTPBasicAuthHandler(pwdmgr)
    opener = urllib.request.build_opener(auth_handler)
    urllib.request.install_opener(opener)
    local_filename, headers = urllib.request.urlretrieve(url)
    

    I'm not entirely sure why the newer code works over the older stuff.

    FYI: the request code never worked either

    response = requests.get('http://...', auth=HTTPBasicAuth('user', 'pass'), stream=True)
    

    I kept getting an Unauthorized http errors