我在python中使用urllib.request尝试从Teamcity下载一些构建信息。这个请求过去没有用户名和密码,但是最近的安全性更改意味着我必须使用用户名和密码。因此,我改变了以下两种解决方案:
尝试1)
url = 'http://<domain>/httpAuth/app/rest/buildTypes/<buildlabel>/builds/running:false?count=1&start=0'
# create a password manager
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
# Add the username and password.
top_level_url = "http://<domain>/httpAuth/app/rest/buildTypes/id:<buildlabel>/builds/running:false?count=1&start=0"
password_mgr.add_password(None, top_level_url, username, password)
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
# create "opener" (OpenerDirector instance)
opener = urllib.request.build_opener(handler)
# use the opener to fetch a URL
opener.open(url)
尝试2
url = 'http://<username>:<password>@<domain>/httpAuth/app/rest/buildTypes/id:buildlabel/builds/running:false?count=1&start=0'
rest_api = urllib.request.urlopen(url)
两者都返回“HTTP Error 401:Unauthorized”。不过,如果我要打印“url”并将此输出复制到浏览器中,则链接工作正常。但是当通过python使用时,我得到了上面的错误。
我在另一个Perl脚本中使用了一些非常相似的东西,这也非常有效。
*在下面解决*
解决了这个问题。
另外,我想下载一个文件。。
其中Url是:
相关问题 更多 >
编程相关推荐