def FormDataEncoder(data): print('in FormDataEncoder') try: pattern = {} pattern['block'] = re.compile(r'------WebKitFormBoundary') pattern['headbody'] = re.compile(r'------WebKitFormBoundary.*\n(?P<header>.*)(?=\n\n)(?P<body>.*)',re.DOTALL) pattern['head'] = re.compile(r'([\n ](?P<key>[ a-zA-Z0-9-]*)[ =:]?[" ](?P<value>.+?)[" ;\n])', re.DOTALL) index = [] res = re.finditer(pattern['block'], data) for i in res: index.append(i.start()) count = 0 file = open('/home/temporary.txt','w') while count <= len(index): count += 1 text = data[index[count-1]:index[count]] res = pattern['headbody'].match(text) res = res.groups() file.write(text) #print ('len:'+str(len(res))) file.close() res2= re.finditer(pattern['head'], res[:-1]) for i in res2: print('i='+str(i)) print('content:'+str(res['body'])) except: traceback.print_exc(file=sys.stdout) 

Log

 ------WebKitFormBoundarybvYPNihBh5rAEBOJ Content-Disposition: form-data; name="action" null ------WebKitFormBoundarybvYPNihBh5rAEBOJ Content-Disposition: form-data; name="photo0"; filename="maksim-grek.jpg" Content-Type: image/jpeg \FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\EC\00Ducky\00\00\00\00\00<\00\00\FF\E1 http://ns.adobe.com/xap/1.0/\00<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.2-c063 53.352624, 2008/07/30-18:12:18 "> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:photoshop="http://ns.adobe.com/photoshop/1.0/" xmp:CreatorTool="Adobe Photoshop CS4 Windows" 

The first is the function for parsing the request, the second is the body of the request. Already a week I can not finish this thing to the end. On the site, everything works fine in 3 stages. The first stage is file enumeration and block search with request attributes and value, the second one breaks the block into 2 parts: in one attribute, in the second value, which is transmitted in the request. The last stage - I divide the first part in the form of key: value. But it just does not work for me in reality, even in parts. I managed to write one expression for the whole file. I tried to connect the cgi module today, but the object it returns is empty. In this case, I want to send an ajax request with fields and files (besides, I didn't manage to put files into an array, because the whole request looks like this: var1, var2, photo0, photo1, .., photoN) . But all this works through tastypie, because now I am writing this function for a deserializer. Please help me, otherwise it’s just a shame to me that the week has passed, but the result is not that 0, but None :)

PS The first template is working, the second is empty, it has not reached the third

  • “I tried to connect the cgi module today, but the object that it returns is empty.” - well, you should probably figure out why it's empty before writing your bikes, isn't it? - andreymal
  • And by the way, django, after all, must parse it all; for some reason you cannot use request.POST and request.FILES or why do you need all this? - andreymal
  • In order to get access to them, I need to do desirialization of data in a tastypie, because it normally accepts only json, and I can only receive multipart / form-data as a binary file. It would be so simple, then, I can assume that I would not leave a new post here - Maxim Stukalo
  • And cgi still worked, but the result was given by the kryakozabrami, where I did not figure it out with the encoding - Maxim Stukalo
  • doesn't dict(request.POST) give anything json-compatible? In general, it seems to me, it’s better to create a separate question in it, tell me what you really want from django and tastypie, but it’s not clear yet) - andreymal

0