首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Python请求和'X-Frame-Options'上传文件:'DENY'

使用Python请求和'X-Frame-Options'上传文件:'DENY'
EN

Stack Overflow用户
提问于 2018-09-21 07:27:34
回答 1查看 0关注 0票数 0

我正在尝试通过python请求库上传文件。这很简单,我可以在其他应用程序中执行此操作。但是在一个安全应用程序中,我有一个如下所示的标题:

代码语言:javascript
复制
{'_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>}

我总是有这个标题。我的上传方法就像:

代码语言:javascript
复制
    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()

为什么'连接':'关闭'?这里有什么问题?我认为我的代码正在运行,但服务器阻止了请求?关于这个问题的任何建议将不胜感激。

在此处输入图像描述
在此处输入图像描述
EN

回答 1

Stack Overflow用户

发布于 2018-09-21 16:37:53

如果upload_file是文件,请使用:

代码语言:javascript
复制
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标头中:

代码语言:javascript
复制
    >>> 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”参数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档