我正在尝试通过python请求库上传文件。这很简单,我可以在其他应用程序中执行此操作。但是在一个安全应用程序中,我有一个如下所示的标题:
{'_content': b'{"fireeyeapis":{"@version":"v2.0.0","description":"Error
while trying to upload the file or parsing
input","httpStatus":400,"message":"Error while trying to upload the file
or parsing input"}}', '_content_consumed': True, '_next': None,
'status_code': 400, 'headers': {'Date': 'Thu, 02 Aug 2018 20:26:58 GMT',
'Server': 'Apache/2', 'X-Content-Type-Options': 'nosniff', 'Cache-
Control': 'no-cache, no-store, must-revalidate, no-cache, no-store',
'Pragma': 'no-cache', 'Expires': '0', 'Content-Type': 'application/json',
'X-Frame-Options': 'DENY', 'X-XSS-Protection': '1; mode=block',
'Connection': 'close', 'Transfer-Encoding': 'chunked'}, 'raw':
<urllib3.response.HTTPResponse object at 0x7f3cf71e2358>, 'url':
'https://xxx.xx.xxx.xx/wsapis/v2.0.0/customioc/feed/add', 'encoding':
None, 'history': [], 'reason': 'Bad Request', 'cookies':
<RequestsCookieJar[]>, 'elapsed': datetime.timedelta(0, 0, 26188),
'request': <PreparedRequest [POST]>, 'connection':
<requests.adapters.HTTPAdapter object at 0x7f3cfde60cc0>}
我总是有这个标题。我的上传方法就像:
def create_custom_feed(self):
headers = self.baseheaders
headers.update({
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
})
payload = {
'feedName': 'ip feed test',
'feedType': 'ip',
'feedAction': 'alert',
'feedSource': 'thirdparty',
'overwrite': 'true',
'filename': "custom_feed"
}
url = self.base_url + "customioc/feed/add"
submission = {'filename': open('custom_feed.txt', 'rb')}
request = requests.post(url, verify=False, headers=headers, files=submission, data=payload)
return request.json()
为什么'连接':'关闭'?这里有什么问题?我认为我的代码正在运行,但服务器阻止了请求?关于这个问题的任何建议将不胜感激。
发布于 2018-09-21 16:37:53
如果upload_file是文件,请使用:
files = {'upload_file': open('file.txt','rb')} values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'} r = requests.post(url, files=files, data=values)
和请求将发送一个多部分表单POST正文,其upload_file字段设置为file.txt文件的内容。
文件名将包含在特定字段的mime标头中:
>>> import requests
>>> open('file.txt', 'wb') # create an empty demo file
<_io.BufferedWriter name='file.txt'>
>>> files = {'upload_file': open('file.txt', 'rb')}
>>> print(requests.Request('POST', 'http://example.com', files=files).prepare().body.decode('ascii'))
--c226ce13d09842658ffbd31e0563c6bd
Content-Disposition: form-data; name="upload_file"; filename="file.txt"
请注意filename =“file.txt”参数。
https://stackoverflow.com/questions/-100002709
复制相似问题