将curl PUT命令转换为Python请求.pu

2024-03-29 14:14:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想打开curl命令:

curl -v -k -X PUT -u user:passwd --data-binary @somefile -H 'Content-Type:text/plain' https://192.168.0.22/dir/subdir

变成一条Python请求.put命令。在

我在这个论坛上看到了很多POST转换的例子,但是“--data binary@somefile”类型的参数似乎没有翻译。在

我至少尝试过以下两种安排:

^{pr2}$

给定的curl命令在命令提示符下工作,但我似乎无法在python中获得正确的语法。在

谁能告诉我下一步该怎么做吗?在

谢谢

q位


Tags: texthttps命令dataputtypedircontent
2条回答

将文件处理程序作为数据发送,而不是保存在dict中

auth = ('user', passwd)
headers = {'Content-Type': 'text/plain'}
somefile=open('somefile','r')
requests.put(uri, auth=auth, headers=headers, data=somefile, verify=False)

或者

^{pr2}$

而且您不需要读取文件内容,requests已经为可流化的data&;files参数执行此操作。在

这是使脚本能够加载文件的代码:

somefile = open('somefile','r')
response = requests.put(uri, auth=auth, headers=headers, files={' data-binary':somefile}, verify=False)

我很感激你的帮助。在

谢谢

q位

相关问题 更多 >