0

我正在将 Django 与 Python 3.5 一起使用,并且我正在尝试从 URL 下载文件,并提供文件名和文件应该下载的路径。我正在使用urlretrieve此操作,但每次都会收到 500 错误。但是在 Postman 中测试时,API 工作正常。谁能帮助我哪里出错了?

headers = {'content-type': 'application/json'}
posts= {
  "filepath":filepath
}
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
url = 'http://'+API_dns+':'+API_pod+'/api/v1/download-file?filepath='+filepath+'&tid='+tid_selected+'&username='+str(request.user)+'&download_zip_file='+zip_file
password_mgr.add_password(None, url, 'admin', 'admin')
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
opener = urllib.request.build_opener(handler)
urllib.request.install_opener(opener)
urllib.request.urlretrieve(url, os.path.join(settings.STATIC_ROOT, "to_download", zip_file))
path = '/static/to_download/'+zip_file
context = {'url':path, 'status':200, 'filename':zip_file}
return JsonResponse(context)
4

0 回答 0