python 用request模块发送cookie,发送多文件。django后台做转发。代码


用request模块发送cookie,发送多文件:

phoneFile=open('2hua.jpg','rb')

phoneFile=phoneFile.read()

emCardFile=open('2hua.jpg','rb')

emCardFile=emCardFile.read()

data=dict(phoneFile=base64.b64encode(phoneFile),

          emCardFile=base64.b64encode(emCardFile),

          vocateFile=base64.b64encode(vocateFile))

cookies =dict(uuid='8877')          

r = requests.post(url,cookies = cookies,data=data)

print(r.requests)


django后台

def up_file(request):

    try:

    phoneFile = request.POST.get('phoneFile')

    emCardFile = request.POST.get('emCardFile')

    vocateFile = request.POST.get('vocateFile')

    # deleteFile = request.POST.get('deleteFile', None)

    phoneFile = base64.b64decode(phoneFile)

    emCardFile= base64.b64decode(emCardFile)

    vocateFile= base64.b64decode(vocateFile)

    url = URL + /upload'

    uuid = request.COOKIES.get('uuid')

    data = get_doctor_status(uuid)

    logger.info('get_doctor_status: %s' % data)

    files = {'phoneFile': ('phoneFile', phoneFile, 'multipart/form-data', {'Expires': '0'}),

              'emCardFile': ('emCardFile', emCardFile, 'multipart/form-data', {'Expires': '0'}),

              'vocateFile': ('vocateFile', vocateFile, 'multipart/form-data', {'Expires': '0'})}

    response = requests.post(url, files=files, data=data, headers=headers).json()

    return response

excpet:

&^^^^^

请自行加入放错机制

你可能感兴趣的:(python)