Important refactoring for web-based functionality

This commit is contained in:
Miroslav Stampar
2012-10-29 15:09:05 +01:00
parent 81ccf28785
commit 5358d85d37
3 changed files with 23 additions and 41 deletions

View File

@@ -24,6 +24,7 @@ import mimetools
import mimetypes
import os
import stat
import StringIO
import sys
import urllib
import urllib2
@@ -52,7 +53,7 @@ class MultipartPostHandler(urllib2.BaseHandler):
try:
for(key, value) in data.items():
if type(value) == file or hasattr(value, 'file'):
if isinstance(value, file) or hasattr(value, 'file') or isinstance(value, StringIO.StringIO):
v_files.append((key, value))
else:
v_vars.append((key, value))
@@ -85,7 +86,7 @@ class MultipartPostHandler(urllib2.BaseHandler):
buf += '\r\n\r\n' + value + '\r\n'
for (key, fd) in files:
file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
file_size = os.fstat(fd.fileno())[stat.ST_SIZE] if isinstance(fd, file) else fd.len
filename = fd.name.split('/')[-1]
contenttype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
buf += '--%s\r\n' % boundary