Trouble with flex file-uploading and twisted.web2 FileSaver(烙铁)

用Flex的FileReference来把文件post到twisted.web2写得服务端,这个看似很简单,却问题多多。

Flex总是报错“IO错误, #2038”

(代码很普通,文档中的实例)

服务段代码:

 

from twisted.web2 import http_headers, resource, / static, server, channel, http, responsecode from twisted.python import util FORMHTML = """ <html> <p>测试用</p> <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="FileUpload" /> <input type="submit" value="Upload"/> </form> </html> """ class MyFileSaver(static.FileSaver): """测试用""" def render(self, req): print req, '--------' if req.files: for fieldName in req.files: print fieldName return http.Response(responsecode.OK, {}, stream='ok') class Toplevel(resource.Resource): addSlash = True def render(self, ctx): return http.Response(stream=FORMHTML) child_upload = MyFileSaver(util.sibpath(__file__, ''), expectedFields=['FileUpload'], allowedTypes=( http_headers.MimeType('image', 'jpeg'), http_headers.MimeType('image', 'png'), http_headers.MimeType('image', 'gif'), ) ) child_swf = static.File(util.sibpath(__file__, 'FileUpload.swf')) site = server.Site(Toplevel()) # Standard twisted application Boilerplate from twisted.application import service, strports application = service.Application("demoserver") s = strports.service('tcp:8080', channel.HTTPFactory(site)) s.setServiceParent(application) 

 

 

无奈之下,sniff了一把。(mac下free:CocoaPacketAnalyzer)

 

Flash Player post 了数据:

 

Packet: 7, Packetlength: 317 bytes, Packet follows: 00000 02 00 00 00 45 00 01 39 da ea 40 00 40 06 00 00 ....E..9..@.@... 00010 7f 00 00 01 7f 00 00 01 fc 2e 1f 90 54 b8 6e 2f ............T.n/ 00020 39 83 39 5c 80 18 ff ff ff 2d 00 00 01 01 08 0a 9.9/.....-...... 00030 3d 8d 67 80 3d 8d 67 80 50 4f 53 54 20 2f 75 70 =.g.=.g.POST /up 00040 6c 6f 61 64 20 48 54 54 50 2f 31 2e 31 0d 0a 48 load HTTP/1.1..H 00050 6f 73 74 3a 20 31 32 37 2e 30 2e 30 2e 31 3a 38 ost: 127.0.0.1:8 00060 30 38 30 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a 080..Connection: 00070 20 63 6c 6f 73 65 0d 0a 55 73 65 72 2d 41 67 65 close..User-Age 00080 6e 74 3a 20 41 64 6f 62 65 20 46 6c 61 73 68 20 nt: Adobe Flash 00090 50 6c 61 79 65 72 20 31 30 0d 0a 58 2d 46 6c 61 Player 10..X-Fla 000a0 73 68 2d 56 65 72 73 69 6f 6e 3a 20 31 30 2c 30 sh-Version: 10,0 000b0 2c 32 32 2c 38 37 0d 0a 41 63 63 65 70 74 2d 54 ,22,87..Accept-T 000c0 79 70 65 73 3a 20 74 65 78 74 2f 2a 0d 0a 43 6f ypes: text/*..Co 000d0 6e 74 65 6e 74 2d 54 79 70 65 3a 20 6d 75 6c 74 ntent-Type: mult 000e0 69 70 61 72 74 2f 66 6f 72 6d 2d 64 61 74 61 3b ipart/form-data; 000f0 20 62 6f 75 6e 64 61 72 79 3d 2d 2d 2d 2d 2d 2d boundary=------ 00100 2d 2d 2d 2d 67 4c 36 49 6a 35 65 69 34 63 48 32 ----gL6Ij5ei4cH2 00110 67 4c 36 47 49 33 63 48 32 67 4c 36 45 66 31 67 gL6GI3cH2gL6Ef1g 00120 4c 36 0d 0a 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 L6..Content-Leng 00130 74 68 3a 20 35 34 37 33 39 0d 0a 0d 0a th: 54739....  

 

眼看着post完了,到了最后,twisted.web2返回:

 

Packet: 63, Packetlength: 388 bytes, Packet follows: 00000 02 00 00 00 45 00 01 80 2b d2 40 00 40 06 00 00 ....E...+.@.@... 00010 7f 00 00 01 7f 00 00 01 1f 90 fc 2e 39 83 39 5c ............9.9/ 00020 54 b9 45 07 80 18 ff ff ff 74 00 00 01 01 08 0a T.E......t...... 00030 3d 8d 67 80 3d 8d 67 80 48 54 54 50 2f 31 2e 31 =.g.=.g.HTTP/1.1 00040 20 34 30 30 20 42 61 64 20 52 65 71 75 65 73 74 400 Bad Request 00050 0d 0a 44 61 74 65 3a 20 54 68 75 2c 20 32 30 20 ..Date: Thu, 20 00060 41 75 67 20 32 30 30 39 20 31 34 3a 34 30 3a 33 Aug 2009 14:40:3 00070 39 20 47 4d 54 0d 0a 43 6f 6e 74 65 6e 74 2d 4c 9 GMT..Content-L 00080 65 6e 67 74 68 3a 20 31 33 38 0d 0a 43 6f 6e 74 ength: 138..Cont 00090 65 6e 74 2d 54 79 70 65 3a 20 74 65 78 74 2f 68 ent-Type: text/h 000a0 74 6d 6c 0d 0a 53 65 72 76 65 72 3a 20 54 77 69 tml..Server: Twi 000b0 73 74 65 64 2f 38 2e 32 2e 30 20 54 77 69 73 74 sted/8.2.0 Twist 000c0 65 64 57 65 62 2f 5b 74 77 69 73 74 65 64 2e 77 edWeb/[twisted.w 000d0 65 62 32 2c 20 76 65 72 73 69 6f 6e 20 38 2e 32 eb2, version 8.2 000e0 2e 30 5d 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a .0]..Connection: 000f0 20 63 6c 6f 73 65 0d 0a 0d 0a 3c 68 74 6d 6c 3e close....<html> 00100 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e 42 61 64 <head><title>Bad 00110 20 52 65 71 75 65 73 74 3c 2f 74 69 74 6c 65 3e Request</title> 00120 3c 2f 68 65 61 64 3e 3c 62 6f 64 79 3e 3c 68 31 </head><body><h1 00130 3e 42 61 64 20 52 65 71 75 65 73 74 3c 2f 68 31 >Bad Request</h1 00140 3e 3c 70 3e 55 6e 65 78 70 65 63 74 65 64 20 64 ><p>Unexpected d 00150 61 74 61 20 6f 6e 20 73 61 6d 65 20 6c 69 6e 65 ata on same line 00160 20 61 73 20 62 6f 75 6e 64 61 72 79 3a 20 27 2d as boundary: '- 00170 2d 27 3c 2f 70 3e 3c 2f 62 6f 64 79 3e 3c 2f 68 -'</p></body></h 00180 74 6d 6c 3e tml>  

 

这个错误很奇怪:

Bad Request

Unexpected data on same line as boundary: '--'

 

spotlight twisted 源码,只有web2, fileupload.py用了这样的字符:

 

def _readBoundaryLine(self): #print "_readBoundaryLine" line = self.stream.readline(size=1024) if isinstance(line, defer.Deferred): line = defer.waitForDeferred(line) yield line line = line.getResult() if line == "--/r/n": # THE END! yield False return elif line != "/r/n": raise MimeFormatError("Unexpected data on same line as boundary: %r" % (line,)) yield True return _readBoundaryLine = defer.deferredGenerator(_readBoundaryLine) 

 

exarkun回信说:


This sounds like it may be related to this ticket:

 http://twistedmatrix.com/trac/ticket/2113

Or it may be another unrelated error.  If you can try the patch attached
to that ticket and comment if it works, that might be helpful.
Development is not currently proceeding very rapidly on Twisted Web2,
though.  You may need to champion the ticket in order to get it
resolved.

 

 

还没实验到底能不能用.

 

你可能感兴趣的:(c,image,Flex,upload,application,import)