gpt4 book ai didi

python - 使用Python requests模块上传文件

转载 作者:行者123 更新时间:2023-12-01 05:02:44 24 4
gpt4 key购买 nike

我需要使用soap端点url加载文件...当我使用下面的代码加载它时,文件正在加载,但它们不是可读的格式...当我使用SOAPUI工具加载时,它会加载正确地...

import requests

xml = '''<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:v1="http://s.sa.com/services/Attachment/v1.0">
<soapenv:Header/>
<soapenv:Body>
<v1:attachment>
<filename>FUZZY.csv</filename>
<data>cid:138641430598</data>
</v1:attachment>
</soapenv:Body>
</soapenv:Envelope>'''
target_url = 'https://s.sa.com:443/soatest/FileAttachmentService'
headers = {'Content-Type': 'text/xml','charset':'utf-8'}
r = requests.post(target_url,data=xml,headers=headers,auth=('3user1',''))
print 'r.text = ', r.text
print 'r.content = ', r.content
print 'r.status_code = ', r.status_code

新变化:-

files = {'file':open('./FUZZY.csv','rb')}
print files
r = requests.post(target_url,files=files,data=xml,headers=headers,auth=('p3user1',''))

错误:

Traceback (most recent call last):
File "soapcall_python.py", line 18, in <module>
r = requests.post(target_url,files=files,data=xml,headers=headers,auth=('p3user1',''))
File "/opt/python2.7/lib/python2.7/site-packages/requests-2.3.0-py2.7.egg/requests/api.py", line 88, in post
return request('post', url, data=data, **kwargs)
File "/opt/python2.7/lib/python2.7/site-packages/requests-2.3.0-py2.7.egg/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/opt/python2.7/lib/python2.7/site-packages/requests-2.3.0-py2.7.egg/requests/sessions.py", line 418, in request
prep = self.prepare_request(req)
File "/opt/python2.7/lib/python2.7/site-packages/requests-2.3.0-py2.7.egg/requests/sessions.py", line 356, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/opt/python2.7/lib/python2.7/site-packages/requests-2.3.0-py2.7.egg/requests/models.py", line 297, in prepare
self.prepare_body(data, files)
File "/opt/python2.7/lib/python2.7/site-packages/requests-2.3.0-py2.7.egg/requests/models.py", line 432, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "/opt/python2.7/lib/python2.7/site-packages/requests-2.3.0-py2.7.egg/requests/models.py", line 109, in _encode_files
raise ValueError("Data must not be a string.")
ValueError: Data must not be a string.

最佳答案

您没有将文件的内容发送到任何地方。您只是发送对服务器可以看到的任何地方都不存在的文件的引用。

作为 SOAP references to attachments 的文档解释说,执行此操作的方法是发送 MIME 多部分消息。如果您使用 CID 引用机制,则 cid 不是任意字符串,它必须与 MIME 信封中邮件的 Content-ID header 相匹配。

POST a Multipart-Encoded File请求文档解释如何在 MIME 请求中将文件内容作为消息发送;简而言之:

with open('FUZZY.csv', 'rb') as f:
files = {'file': f}
r = requests.post(target_url,
data=xml, headers=headers, auth=('3user1',''),
files=files)

但是,这种简单的方法无法让您访问将在后台为您的消息生成的 Content-ID。因此,如果您想使用 CID 引用机制,则需要手动生成 MIME 信封(例如,通过使用 email.mail.MIMEMultipart )并将整个内容作为 data 字符串发送。

关于python - 使用Python requests模块上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25731710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com