python
  • python-requests
  • basic-authentication
  • 2017-02-15 60 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

    相關問題