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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:51:44+00:00 2026-06-03T04:51:44+00:00

I have a server that is trying to stream some content to a client

  • 0

I have a server that is trying to stream some content to a client (which is Kenneth Reitz excellent requests library) – (Props to toastdriven.com for code below). Note that in a browser, it works as expected.

from gevent import monkey
monkey.patch_all()

import datetime
import time
from gevent import Greenlet
from gevent import pywsgi
from gevent import queue

import json

def current_time(body):
    current = start = datetime.datetime.now()
    end = start + datetime.timedelta(seconds=60)

    while current < end:
        current = datetime.datetime.now()
        message = json.dumps({'time': current.strftime("%Y-%m-%d %I:%M:%S")})
        body.put(message)
        time.sleep(1)

    body.put('</body></html>')
    body.put(StopIteration)

def handle(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    body = queue.Queue()
    g = Greenlet.spawn(current_time, body)
    return body

server = pywsgi.WSGIServer(('127.0.0.1', 1234), handle)
print "Serving on http://127.0.0.1:1234..."
server.serve_forever()

And a client:

import sys
import requests
import json

my_config = {'verbose': sys.stdout}
r = requests.get('http://127.0.0.1:1234/', config=my_config)

for line in r.iter_lines():
    print json.loads(line)

I dont’ understand why the json lines arent’ showing up in the terminal (OSX). When I ctrl-c the responses are dumped to the screen.

If I do:

for line in r.iter_content()

I get the json, a character on each line, streamed as expected.

Any ideas?

  • 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-03T04:51:46+00:00Added an answer on June 3, 2026 at 4:51 am

    Your client is probably running in a terminal window that is buffered. After each print, try adding sys.stdout.flush() to flush the output buffer.

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

Sidebar

Related Questions

I have a server that has several virtual machines running on it. I'm trying
i have a sql server that i use and i am trying to code
Alright, I have a server that serves a motion-jpeg stream over http. What I
I am having some problems figuring out this issue. I have a server that
I'm trying to write some python code which can create multipart mime http requests
I am trying to rewrite some server php code which logs into a website,
I have a WCF service and Im looking to stream some files from server
I'm trying to send some data from a C++ server to a C# client.
I'm writing RTSP server and have some problems. I'm using VLC as client. Server
I have a server that is running Ubuntu Linux Server Edition. I once had

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.