python
  • python-requests
  • basic-authentication
  • 2017-02-15 45 views 0 likes 
    0

    我想把一个文件放到WebDav启用的URL。 的代码看起来是这样的:蟒蛇3得到401使用requests.put

    headers = {'Authorization':'Basic', 'username': 'doc_iconx', 'password': 'doc_iconx'} 
        id = "SOMEID" 
        pw = "SOMEPW" 
        try: 
         url = 'https://mywebsite.com/Dir/' 
         files = {'upload_file': open(fileName, 'rb')} 
         r = requests.put(url,auth=HTTPDigestAuth(id,pw), files=files, headers={'User-Agent': 'Mozilla' 
        }) 
    

    我回去:

    <title>401 Unauthorized</title> 
    </head><body> 
    <h1>Unauthorized</h1> 
    <p>This server could not verify that you 
    are authorized to access the document 
    requested. Either you supplied the wrong 
    credentials (e.g., bad password), or your 
    browser doesn't understand how to supply 
    the credentials required.</p> 
    </body></html> 
    

    我知道ID /密码是好的,因为我可以使用curl

    任何想法做一个放?

    +0

    从我看到的情况来看,您应该使用'HTTPBasicAuth'而不是'HTTPDigestAuth'。你也提到'post',但在你的例子中你正在做一个'put'。 – sal

    +0

    感谢您指出我的错字。我纠正了描述为.put。 – user3670332

    +0

    尝试使用HTTPBasicAuth。现在我得到了:409客户端错误:URL冲突:https://content-qa.homedepot.com/IconX/Report/ – user3670332

    回答

    0

    由于您的认证方案是使用Basic,所有你需要做的就是用HTTPBasicAuth而不是HTTPDigestAuth

    r = requests.put(url,auth=HTTPBasicAuth(id,pw), files=files, headers={'User-Agent': 'Mozilla'}) 
    

    针对requests实际上有甚至一个快捷方式,不指定模式:

    r = requests.put(url,auth=(id,pw), files=files, headers={'User-Agent': 'Mozilla'}) 
    
    0

    我有两个不同的问题正在进行。 Sal,纠正了我的验证错误。第二个错误是一个愚蠢的用户错误。我需要将我想要上传的文件名添加到URL的末尾。它构建的方式是试图创建一个名为Report的文件。但是Report是一个现有的目录,我打算写这个文件。

    +0

    很高兴它的工作。请考虑接受我的答案,因为它解决了您的原始发布问题。 – sal

    相关问题