views:

36

answers:

1

How could i upload a file to a server without using FieldStorage in python?

+1  A: 

Here is a toy program snippet that should help get you started. Try reading RFC 1867 as well for more guidance.


#!/usr/bin/python

import os
import sys

buf = sys.stdin.read(512)

print "Content-type: text/html\n\n";
print '<html>'
print '''
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="f">
<input type="submit">
</form>
'''
print buf

print '</html>'

You can use os.environ.items() to get a list of environment variables, notably CONTENT_LENGTH and CONTENT_TYPE (specifically the boundary key/pair) so you know where the demarcation points are for the uploaded content.

jtp