Python Basehttpserver File Upload With Maxfile-size
If the file is to big i dont want to download it to my server and then delete it, I just want to tell the user that the file is to big. this code almost accomplish this. if the fi
Solution 1:
You have got to read all request data in any case. Otherwise HTTP client (browser) just don't get it.
In first case when file is too big, you can just read and ignore data.
Here is updated StoreHandler:
class StoreHandler(BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers['content-length'])
print length
if length > 10000000:
print"file to big"read = 0whileread < length:
read += len(self.rfile.read(min(66556, length - read)))
self.respond("file to big")
returnelse:
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
filename = form['file'].filename
data = form['file'].file.read()
open("/tmp/%s"%filename, "wb").write(data)
printself.headers['content-length']
self.respond("uploaded %s, thanks")
Post a Comment for "Python Basehttpserver File Upload With Maxfile-size"