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 7962299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:18:08+00:00 2026-06-04T05:18:08+00:00

I’m using Python Flask + nginx with FCGI. On some requests, I have to

  • 0

I’m using Python Flask + nginx with FCGI.

On some requests, I have to output large responses. Usually those responses are fetched from a socket. Currently I’m doing the response like this:

response = []
while True:
    recv = s.recv(1024)
    if not recv: break
    response.append(recv)
s.close()
response = ''.join(response)

return flask.make_response(response, 200, {
                                           'Content-type': 'binary/octet-stream',
                                           'Content-length': len(response),
                                           'Content-transfer-encoding': 'binary',
                                           })

The problem is I actually do not need the data. I also have a way to determine the exact response length to be fetched from the socket. So I need a good way to send the HTTP headers, then start outputing directly from the socket, instead of collecting it in memory and then supplying to nginx (probably by some sort of a stream).

I was unable to find the solution to this seemingly common issue. How would that be achieved?

Thank you!

  • 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-04T05:18:09+00:00Added an answer on June 4, 2026 at 5:18 am

    if response in flask.make_response is an iterable, it will be iterated over to produce the response, and each string is written to the output stream on it’s own.

    what this means is that you can also return a generator which will yield the output when iterated over. if you know the content length, then you can (and should) pass it as header.

    a simple example:

    from flask import Flask
    app = Flask(__name__)
    import sys
    import time
    import flask
    
    @app.route('/')
    def generated_response_example():
        n = 20
        def response_generator():
            for i in range(n):
                print >>sys.stderr, i
                yield "%03d\n" % i
                time.sleep(.2)
    
        print >>sys.stderr, "returning generator..."
        gen = response_generator()
    
        # the call to flask.make_response is not really needed as it happens imlicitly 
        # if you return a tuple.
        return flask.make_response(gen ,"200 OK", {'Content-length': 4*n})
    
    if __name__ == '__main__':
        app.run()
    

    if you run this and try it in a browser, you should see a nice incemental count…

    (the content type is not set because it seems if i do that my browser waits until the whole content has been streamed before rendering the page. wget -qO - localhost:5000 doesn’t have this problems.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the

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.