[Twisted-Python] Doing HTTP file uploads (multipart forms)

Laurens Van Houtven _ at lvh.cc
Fri Jul 6 04:47:13 EDT 2012


I have some code that takes file uploads from browsers. I'm trying to write a test for it, so now I need to get Twisted to do file uploads like browsers do. I think my code (not test code) essentially works, by manually trying it with a browser. I can't get the functional test part to work.

I have reduced the problem to what I think is a SSCCE. render_POST drops into a debugger to easily inspect the received request.

Here's the code: https://gist.github.com/3058974

When debugging this with wireshark I found an obvious culprit: there's some random junk in front of it (3 ASCII hex digits and a CRLF) and some junk at the end (CRLF and an ASCII "0", although I'm not sure if that CRLF is junk). Wireshark reports some broken TCP packets (PCAP attached). I have no idea why that happens. Packets were captured with:

    tcpdump -i lo0 -nn -s0 -w sample.pcap port 8080

and analyzed with a recent version of Wireshark (1.6.2, SVN rev 38931). The analyzed TCP stream is also attached.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: tcpdata.txt
Url: http://twistedmatrix.com/pipermail/twisted-python/attachments/20120706/e6354a10/attachment.txt 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.pcap
Type: application/octet-stream
Size: 1479 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20120706/e6354a10/attachment.obj 
-------------- next part --------------

If I look at the request in the debugger (request_POST *DOES* get called?):

 - it has an empty request.args, instead of having the expected keys "a", "b", "f"
 - request.content.getvalue() has the data you see in tcpdata.txt: it starts with "'1e7\r\n------------" even though I obviously would like it to start with just the dashes


More information about the Twisted-Python mailing list