我正在使用Python中的请求库将文件上传到服务器。我阅读了其文档和一些stackoverflow问题,并实现了以下代码:
url = "http://example.com/file.csv"
id = "user-id"
password = "password"
headers = {'content-type': 'application/x-www-form-urlencoded'}
with open(file_path, 'rb') as f:
response = requests.post(url=url, files={'file':f}, auth=HTTPBasicAuth(username=id, password=password),headers=headers)
但是此代码无法正常工作,response.status_code返回405,response.reason返回Method Not Allowed
。当我在终端上使用curl命令上传文件时,它可以正常工作并且文件被上传:
curl -u user-id:password -T file/path/on/local/machine/file.csv "http://example.com/file.csv"
有人可以帮忙吗?
相关问题在这里。实际上,curl --upload-file
执行PUT
不是POST
。如果要模仿卷曲的作用,则可以尝试:
with open(file_path, 'rb') as f:
response = requests.put(url=url, files={'file':f}, auth=HTTPBasicAuth(username=id, password=password), headers=headers)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句