Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8329755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:54:01+00:00 2026-06-09T01:54:01+00:00

I have the following code: def application(env, start_response): path = process(env) fh = open(path,’r’)

  • 0

I have the following code:

def application(env, start_response):
    path = process(env)
    fh = open(path,'r')
    start_response('200 OK', [('Content-Type','application/octet-stream')])
    return fbuffer(fh,10000)


def fbuffer(f, chunk_size):
    '''Generator to buffer file chunks'''  
    while True:
        chunk = f.read(chunk_size)      
        if not chunk: break
        yield chunk

I’m not sure that it’s right but the scraps of information I’ve found on the internet have led me to think it ought to work. Basically I want to stream a file out in chunks, and to do that I’m passing a generator back from my application function. However this only prints out the headers and doesn’t actually send back any data, can anyone tell me why this is?

Alternatively, if this is completely wrong, what’s the best way to do this? I can’t buffer the whole file in memory as the files I’ll be working with are potentially gigabytes large.

Tertiary question: What’s the best way to close the file once I’m done outputting it? In the code I posted I can’t see anyway to actually close the file.

(I’m running python 3.2.3 with uWSGI 1.2.4)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:54:02+00:00Added an answer on June 9, 2026 at 1:54 am

    Without some care, uwsgi is careful not to allow errors to leak, but a if you run your application in a stricter implementation, say the one provided with python as wsgiref.simple_server, you can more easily see the problem.

    Serving <function application at 0xb65848> http://0.0.0.0:8000
    Traceback (most recent call last):
      File "/usr/lib64/python3.2/wsgiref/handlers.py", line 138, in run
        self.finish_response()
      File "/usr/lib64/python3.2/wsgiref/handlers.py", line 179, in finish_response
        self.write(data)
      File "/usr/lib64/python3.2/wsgiref/handlers.py", line 264, in write
        "write() argument must be a bytes instance"
    AssertionError: write() argument must be a bytes instance
    localhost.localdomain - - [04/Aug/2012 16:27:08] "GET / HTTP/1.1" 500 59
    

    The problem is that wsgi requires that data transmitted to and from the HTTP gateway must be served as bytes, but when you use open(path, 'r'), python 3 conveniently converts the data read to unicode, what in python 3 is str, using the default encoding.

    changing

    fh = open(path, 'r')
    

    to

    fh = open(path, 'rb')
    #                 ^
    

    fixes it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my controller/request-handler, I have the following code: def monkey(self, **kwargs): cherrypy.response.headers['Content-Type'] = application/json
I have the following Python (3.2) code: from pygame import * class Application: def
So, I have the following code: def LSCPHandler.send_message(message, hostname, port) s = TCPSocket.open(hostname, port)
I have following code in my django application. class Status(object): def __init__(self, id, desc):
I have following code: def whatever(url, data=None): req = urllib2.Request(url) res = urllib2.urlopen(req, data)
I have the following code: def maturities InfoItem.find_all_by_work_order(self.work_order).map(&:maturity) end I was thinking about changing
I have the following code: def query(self,query): lock = QMutexLocker(self.mutex) reply = self.conn.query(query) if
I have the following code: def add_record(self,values): self.sql.execute(INSERT INTO TEST VALUES (?,?) % values)
i have the following code def get(self): date = datetime.date.today() loc_query = Location.all() last_cursor
I have the following code: def input_scores(): scores = [] y = 1 for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.