Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用多个数据参数发送http请求_Python_Http_Flask_Python Requests - Fatal编程技术网

Python 如何使用多个数据参数发送http请求

Python 如何使用多个数据参数发送http请求,python,http,flask,python-requests,Python,Http,Flask,Python Requests,运行flask_server.py时: from flask import Flask, request, Response import logging logging.basicConfig(level=logging.DEBUG, format='%(levelname)s-%(message)s') app = Flask(__name__) @app.route('/test', methods=['GET','POST']) def

运行
flask_server.py
时:

from flask import Flask, request, Response
import logging

logging.basicConfig(level=logging.DEBUG, 
                    format='%(levelname)s-%(message)s')

app = Flask(__name__)


@app.route('/test', methods=['GET','POST'])
def route():   
    logging.info('get_json: %s : %s' % (request.get_json(), type(request.get_json())))
    logging.info('files: %s : %s' % (request.files, type(request.files)))
    return Response()

if __name__ == '__main__':
    app.run('0.0.0.0', 5000)
使用
requests发送http
request
。post
方法将Python
dictionary
作为
json
参数提供给它:

import json, requests    
dictionary = {"file": {"url": "https://bootstrap.pypa.io/get-pip.py"}}  
response = requests.post("http://127.0.0.1:5000/test", json=dictionary)
Flask服务器记录它使用
Flask.request.get_json
方法获取字典:

root - INFO - get_json: {u'file': {u'url': u'https://bootstrap.pypa.io/get-pip.py'}} : <type 'dict'>
root - INFO - files: ImmutableMultiDict([]) : <class 'werkzeug.datastructures.ImmutableMultiDict'>
response = requests.post("http://127.0.0.1:5000/test",
                         json=dictionary, 
                         files=files)
Flask服务器日志:

root - INFO - get_json: None : <type 'NoneType'>
root - INFO - files: ImmutableMultiDict([('file_slot_1', <FileStorage: u'any_file.txt' (None)>)]) : <class 'werkzeug.datastructures.ImmutableMultiDict'>
服务器记录它获取了文件,但没有获取
json
字典

是否可以发送一个
请求
并向其提供多个数据参数:例如“json”和“files”?

您可以使用
文件
参数和列表进行发送。但是,您的json数据需要位于其中一个文件中。这是因为,即使您在第一个请求中只是执行了
json=dictionary
,它实际上还是在头中发送
Content-Type:application/json
。当您想要发送多个部件时,需要通过
文件=
使用
多部件/表格数据

此处的目标名称假定为
targets
。如果您的Flask应用程序是上传的接收者,您可以自行制作。

您可以使用
文件
参数和列表发送。但是,您的json数据需要位于其中一个文件中。这是因为,即使您在第一个请求中只是执行了
json=dictionary
,它实际上还是在头中发送
Content-Type:application/json
。当您想要发送多个部件时,需要通过
文件=
使用
多部件/表格数据

此处的目标名称假定为
targets
。如果你的Flask应用程序是上传的接收者,你可以自己制作

response = requests.post("http://127.0.0.1:5000/test",
                         json=dictionary, 
                         files=files)
json_data = json.dumps({"file": {"url": "https://bootstrap.pypa.io/get-pip.py"}})
multiple_files = [
    ('targets', ('data.json', json_data, 'application/json')),
    ('targets', ('key_1', open('/any_file.txt', 'rb'), 'text/plain'))
]
response = requests.post(url, files=multiple_files)