it might be some error in http://docs.webob.org/en/latest/file-example.html

21 views
Skip to first unread message

benzwt

unread,
Feb 20, 2012, 4:24:53 AM2/20/12
to Paste Users
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 ?

Sergey Schetinin

unread,
Feb 20, 2012, 4:36:28 AM2/20/12
to benzwt, Paste Users
I don't see any references to req.app_iter in the documentation.

> --
> You received this message because you are subscribed to the Google Groups "Paste Users" group.
> To post to this group, send email to paste...@googlegroups.com.
> To unsubscribe from this group, send email to paste-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/paste-users?hl=en.
>

Reply all
Reply to author
Forward
0 new messages