繁体   English   中英

将curl PUT命令转换为Python request.put

[英]Transform curl PUT Command into Python request.put

我想打开这个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 request.put命令中。

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

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

auth = ('user', passwd)
headers = {'Content-Type': 'text/plain'}
data = '-v -k -X --data-binary @somefile'
requests.put(uri, auth=auth, headers=headers, data=data, verify=False)


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

给定的curl命令可以在命令提示符下运行,但是我似乎无法将正确的语法导入python。

谁能阐明我下一步可能会尝试的方法?

谢谢,

q位

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

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

要么

file = {'file': ('somefile', open('somefile','r'), 'text/plain' )}
requests.put(url, auth=auth, headers=headers, files=file, verify=False)

而且您不需要读取文件内容, requests已经为可流datafiles参数做了。

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

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

感谢您的帮助。

谢谢,

q位

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM