Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3使用requests.put获取401_Python_Python Requests_Basic Authentication - Fatal编程技术网

Python3使用requests.put获取401

Python3使用requests.put获取401,python,python-requests,basic-authentication,Python,Python Requests,Basic Authentication,我正在尝试将文件放入启用WebDav的URL。 代码如下所示: headers = {'Authorization':'Basic', 'username': 'doc_iconx', 'password': 'doc_iconx'} id = "SOMEID" pw = "SOMEPW" try: url = 'https://mywebsite.com/Dir/' files = {'upload_file': open(fileNam

我正在尝试将文件放入启用WebDav的URL。 代码如下所示:

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>
401未经授权
未经授权
此服务器无法验证您是否
您有权访问该文档
请求。要么你提供的是错误的
凭据(例如,错误的密码)或您的
浏览器不了解如何提供
所需的凭据

我知道ID/密码很好,因为我可以使用curl进行put


有什么想法吗?

因为您的身份验证方案使用的是
Basic
,所以您只需使用
HTTPBasicAuth
而不是
HTTPDigestAuth

r = requests.put(url,auth=HTTPBasicAuth(id,pw), files=files, headers={'User-Agent': 'Mozilla'})
通过不指定模式,
请求
实际上甚至有一个快捷方式:

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

我有两个不同的问题。萨尔,更正了我的授权错误。第二个错误是一个愚蠢的用户错误。我需要将我想要上传的文件名附加到URL的末尾。它的构造方式是尝试创建一个名为Report的文件。然而,报告是一个现有的目录,我打算在其中写入文件

在我看来,您可能应该使用
HTTPBasicAuth
而不是
HTTPDigestAuth
。您还提到了
post
,但在您的示例中,您正在做一个
put
。感谢您指出我的打字错误。我更正了使用HTTPBasicAuth.put.trusted的描述。现在我得到一个:409客户端错误:url冲突:如果你想上传一个文件,那么你应该使用
post
作为方法。post对于我们的特定用法不起作用。该网站正在使用webdav,并期待一个PUT。很高兴一切正常。请考虑接受我的回答,因为它解决了你原来发布的问题。