0

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.

push 22
  • 1,172
  • 3
  • 15
  • 34

3 Answers3

0

You can use a lib like requests that let's you put the basic auth as a param, see more here: http://docs.python-requests.org/en/latest/user/authentication/#basic-authentication

import requests
from requests.auth import HTTPBasicAuth
import shutil

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

with open('filename.zip', 'wb') as out_file:
    shutil.copyfileobj(response.raw, out_file)
bakkal
  • 54,350
  • 12
  • 131
  • 107
0

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

push 22
  • 1,172
  • 3
  • 15
  • 34
0

Obtaining Artifacts from a Build Script

import getpass
import subprocess

USERNAME = getpass.getuser()
PWD = getpass.getpass(prompt='PWD:', stream=None)
subprocess.run(['wget','http://'+USERNAME+':'+'PWD'+'@SERVER/httpAuth/repository/downloadAll/dood_dad/latest.lastSuccessful'])
Max
  • 41
  • 3