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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:03:07+00:00 2026-06-16T21:03:07+00:00

I have an http response from urllib response = urllib2.urlopen(‘http://python.org/’) Eventually, I want to

  • 0

I have an http response from urllib

response = urllib2.urlopen('http://python.org/')

Eventually, I want to be able to seek() within the response (at least to the beginning). So I want to be able to have code like this:

print result.readline()
result.seek(0)
print result.readline()

The simplest solution to this problem is StringIO or io.BytesIO like this:

result = io.BytesIO(response.read())

However, the thing is that the resources I want to request tend to be very large and I want to start working with them (parse…) before the whole download is finished. response.read() is blocking. I’m looking for a non-blocking solution.

The ideal code would read(BUFFER_SIZE) from the resource and whenever more content is needed, just request more from the response. I’m basically looking for a wrapper class that can do that. Oh, and I need a file like object.

I thought, I could write something like:

base = io.BufferedIOBase(response)
result = io.BufferedReader(base)

However, it turns out that this does not work and I have tried different classes from the io module but couldn’t get it working. I’m happy with any wrapper class that has the desired behaviour.

  • 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-16T21:03:08+00:00Added an answer on June 16, 2026 at 9:03 pm

    I wrote my own wrapper class which preserves the first chunk of data. This way I can seek back to the beginning, analyze the encoding, file type and other things. This class solves the problem for me and should be simple enough to adapt to other use cases.

    class BufferedFile(object):
        ''' A buffered file that preserves the beginning of a stream up to buffer_size
        '''
        def __init__(self, fp, buffer_size=1024):
            self.data = cStringIO.StringIO()
            self.fp = fp
            self.offset = 0
            self.len = 0
            self.fp_offset = 0
            self.buffer_size = buffer_size
    
        @property
        def _buffer_full(self):
            return self.len >= self.buffer_size
    
        def readline(self):
            if self.len < self.offset < self.fp_offset:
                raise BufferError('Line is not available anymore')
            if self.offset >= self.len:
                line = self.fp.readline()
                self.fp_offset += len(line)
    
                self.offset += len(line)
    
                if not self._buffer_full:
                    self.data.write(line)
                    self.len += len(line)
            else:
                line = self.data.readline()
                self.offset += len(line)
            return line
    
        def seek(self, offset):
            if self.len < offset < self.fp_offset:
                raise BufferError('Cannot seek because data is not buffered here')
            self.offset = offset
            if offset < self.len:
                self.data.seek(offset)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to Python, but I'm trying to...retrieve data from a site: import urllib.request response
Overview I am using urlopen from the Python 2.7.1 urllib2 package to do a
I have an outgoing http endpoint in Mule 3.2.1. As a response to my
We have the following example in node.js var http = require('http'); http.createServer(function(request, response) {
I have a strange bug when trying to urlopen a certain page from Wikipedia.
I have the following simple code: import urllib2 import sys sys.path.append('../BeautifulSoup/BeautifulSoup-3.1.0.1') from BeautifulSoup import
Here I want to make some modificatins for my setting. I want response from
In python I have copied a webpage and want to get all occurrences of
I want to upload and get the result from this website. http://cello.life.nctu.edu.tw/ I tried
I have the following code to open and read URLs: html_data = urllib2.urlopen(req).read() and

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.