Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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请求”;邮政「;将服务器设置为二进制文件_Python_File_Post_Flask_Binary - Fatal编程技术网

使用“编写python请求”;邮政「;将服务器设置为二进制文件

使用“编写python请求”;邮政「;将服务器设置为二进制文件,python,file,post,flask,binary,Python,File,Post,Flask,Binary,我的客户端使用以下代码将文件和一些json上传到flask服务器: datas = {'CurrentMail': "AA", 'STRUserUUID1': "BB", 'FirstName': "ZZ", 'LastName': "ZZ", 'EE': "RR", 'JobRole': "TT" } #sending user infos to app server using python "requests" url = "http://10.100.2.6:80/cust

我的客户端使用以下代码将文件和一些json上传到flask服务器:

datas = {'CurrentMail': "AA", 'STRUserUUID1': "BB", 'FirstName': "ZZ",     'LastName': "ZZ",  'EE': "RR", 'JobRole': "TT"  }

#sending user infos to app server using python "requests"
url = "http://10.100.2.6:80/customerupdate"
def send_request():
    payload = datas
    local_file_to_send = 'user_picture.jpg' 
    files = {
     'json': (None, json.dumps(payload), 'application/json'),
     'file': (os.path.basename(local_file_to_send), open(local_file_to_send, 'rb'), 'application/octet-stream')
}
r = requests.post(url, files=files)

send_request()
  TypeError: argument 1 must be convertible to a buffer, not ImmutableMultiDict
在我的服务器上

在服务器端:

import sys, os, logging, time, datetime, json, uuid, requests, ast
from flask import Flask, request , render_template
from werkzeug import secure_filename
from werkzeug.datastructures import ImmutableMultiDict
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)
app.debug = True

class OPERATIONS(Resource):
    @app.route('/',methods=['GET'])
    def hello_world():
        return 'Hello World!'

    @app.route('/customerupdate',methods=['GET','POST'])
    def customerupdate():
        print "************DEBUG 1 ***********"
        RequestValues = request.values
        print RequestValues
        print "************DEBUG 2 ***********"
        RequestForm = request.form
        print RequestForm
        print "************DEBUG 2-1 ***********"
        so = RequestForm
        json_of_metadatas = so.to_dict(flat=False)
        print json_of_metadatas
        print "************DEBUG 2-2 ***********"
        MetdatasFromJSON = json_of_metadatas['json']
        print MetdatasFromJSON          
        print "************DEBUG 2-3 ***********"
        MetdatasFromJSON0 = MetdatasFromJSON[0]
        print MetdatasFromJSON0
        print "************DEBUG 3-5 ***********"
        strMetdatasFromJSON0 = str(MetdatasFromJSON0)
        MetdatasDICT = ast.literal_eval(strMetdatasFromJSON0)
        print MetdatasDICT
        print "************DEBUG 3-5 ***********"
        for key in MetdatasDICT :
            print "key: %s , value: %s" % (key, MetdatasDICT[key])
        print "************DEBUG 4 ***********"
        f = request.files['file']
        f.save(secure_filename(f.filename))
        print "FILE SAVED LOCALY"
        return 'JSON of customer posted'
所以我可以在我的服务器上保存一些元数据和文件本身。我现在想做的是将完整的“POST”保存为原始二进制文件

如前所述,其中有一些线程,因此,我尝试使用:

        # test 1
        foo = request.get_data()
        #TEST 2
        foo = request.files
        #test 3
        foo = request.stream.read()

        f = request.files['file']
        fname = f.filename
        #Using Hexa for the binary name and prefix bin
        fname_hexa = fname.encode("hex")
        bin_file_name="%s_%s"%("bin",fname_hexa)
        with open(bin_file_name, 'wb') as output_file_bin:
            output_file_bin.write(**foo**)
            output_file_bin.close()
但是我得到一个空文件,不管用什么方法来纠正“foo”:get_data()和stream.read() 如果我只使用“文件”,我从flask服务器收到一条错误消息:

datas = {'CurrentMail': "AA", 'STRUserUUID1': "BB", 'FirstName': "ZZ",     'LastName': "ZZ",  'EE': "RR", 'JobRole': "TT"  }

#sending user infos to app server using python "requests"
url = "http://10.100.2.6:80/customerupdate"
def send_request():
    payload = datas
    local_file_to_send = 'user_picture.jpg' 
    files = {
     'json': (None, json.dumps(payload), 'application/json'),
     'file': (os.path.basename(local_file_to_send), open(local_file_to_send, 'rb'), 'application/octet-stream')
}
r = requests.post(url, files=files)

send_request()
  TypeError: argument 1 must be convertible to a buffer, not ImmutableMultiDict