Hi WEBOBer,
I'm new to both python and webob.
I have just read the doc
http://docs.webob.org/en/latest/file-example.html
and try to reproduce the example.
According to the instruction, I wrote my code as follows:
************************************************
from webob import Request, Response
import os
import mimetypes
class FileApp(object):
def __init__(self, filename):
self.filename = filename
def __call__(self, environ, start_response):
res = self.make_response(self.filename)
return res(environ, start_response)
def get_mimetype(self, filename):
type, encoding = mimetypes.guess_type(filename)
# We'll ignore encoding, even though we shouldn't really
return type or 'application/octet-stream'
def make_response(self, filename):
res = Response(content_type=self.get_mimetype(filename)
,conditional_response=True)
res.app_iter = FileIterable(filename)
res.content_length = os.path.getsize(filename)
return res
class FileIterable(object):
def __init__(self, filename, start=None, stop=None):
print start, stop
self.filename = filename
self.start = start
self.stop = stop
def __iter__(self):
return FileIterator(self.filename, self.start, self.stop)
def app_iter_range(self, start, stop):
return self.__class__(self.filename, start, stop)
class FileIterator(object):
chunk_size = 4096
def __init__(self, filename, start=None, stop=None):
print "dddd"
self.filename = filename
self.fileobj = open(self.filename, 'rb')
if start:
self.fileobj.seek(start)
if stop is not None:
self.length = stop - start
else:
self.length = None
def __iter__(self):
return self
def next(self):
if self.length is not None and self.length <= 0:
raise StopIteration
chunk = self.fileobj.read(self.chunk_size)
if not chunk:
raise StopIteration
if self.length is not None:
self.length -= len(chunk)
if self.length < 0:
# Chop off the extra:
chunk = chunk[:self.length]
return chunk
__next__ = next # py3 compat
fn = os.path.join(os.curdir, 'test.txt')
app = FileApp(fn)
req = Request.blank("/")
req.range = (0, 5)
req.app_iter
print req.get_response(app)
***************************************
But I got,
Traceback (most recent call last):
File "FileServing.py", line 71, in <module>
req.app_iter
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/
lib/python2.7/site-packages/webob/request.py", line 1238, in
__getattr__
raise AttributeError(attr)
AttributeError: app_iter
After that I removed the req.app_iter, and thing ran normal.
Could someone explain why me system failed by adding req.app_iter in
the code ?