Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
it might be some error in http://docs.webob.org/en/lates t/file-example.html
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
benzwt  
View profile  
 More options Feb 20 2012, 4:24 am
From: benzwt <ben...@gmail.com>
Date: Mon, 20 Feb 2012 01:24:53 -0800 (PST)
Local: Mon, Feb 20 2012 4:24 am
Subject: it might be some error in http://docs.webob.org/en/latest/file-exa mple.html
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 ?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sergey Schetinin  
View profile  
 More options Feb 20 2012, 4:36 am
From: Sergey Schetinin <mal...@gmail.com>
Date: Mon, 20 Feb 2012 11:36:28 +0200
Local: Mon, Feb 20 2012 4:36 am
Subject: Re: [Paste] it might be some error in http://docs.webob.org/en/latest/file-exa mple.html
I don't see any references to req.app_iter in the documentation.

On 20 February 2012 11:24, benzwt <ben...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »