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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:02:57+00:00 2026-06-13T21:02:57+00:00

Using python-requests and python-magic, I would like to test the mime-type of a web

  • 0

Using python-requests and python-magic, I would like to test the mime-type of a web resource without fetching all its content (especially if this resource happens to be eg. an ogg file or a PDF file). Based on the result, I might decide to fetch it all. However calling the text method after having tested the mime-type only returns what hasn’t been consumed yet. How could I test the mime-type without consuming the response content?

Below is my current code.

import requests
import magic


r = requests.get("http://www.december.com/html/demo/hello.html", prefetch=False)
mime = magic.from_buffer(r.iter_content(256).next(), mime=True)

if mime == "text/html":
    print(r.text)  # I'd like r.text to give me the entire response content

Thanks!

  • 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-13T21:02:58+00:00Added an answer on June 13, 2026 at 9:02 pm

    Note: at the time this question was asked, the correct method to fetch only headers stream the body was to use prefetch=False. That option has since been renamed to stream and the boolean value is inverted, so you want stream=True.

    The original answer follows.


    Once you use iter_content(), you have to continue using it; .text indirectly uses the same interface under the hood (via .content).

    In other words, by using iter_content() at all, you have to do the work .text does by hand:

    from requests.compat import chardet
    
    r = requests.get("http://www.december.com/html/demo/hello.html", prefetch=False)
    peek = r.iter_content(256).next()
    mime = magic.from_buffer(peek, mime=True)
    
    if mime == "text/html":
        contents = peek + b''.join(r.iter_content(10 * 1024))
        encoding = r.encoding
        if encoding is None:
            # detect encoding
            encoding = chardet.detect(contents)['encoding']
        try:
            textcontent = str(contents, encoding, errors='replace')
        except (LookupError, TypeError):
            textcontent = str(contents, errors='replace')
        print(textcontent)
    

    presuming you use Python 3.

    The alternative is to make 2 requests:

    r = requests.get("http://www.december.com/html/demo/hello.html", prefetch=False)
    mime = magic.from_buffer(r.iter_content(256).next(), mime=True)
    
    if mime == "text/html":
         print(r.requests.get("http://www.december.com/html/demo/hello.html").text)
    

    Python 2 version:

    r = requests.get("http://www.december.com/html/demo/hello.html", prefetch=False)
    peek = r.iter_content(256).next()
    mime = magic.from_buffer(peek, mime=True)
    
    if mime == "text/html":
        contents = peek + ''.join(r.iter_content(10 * 1024))
        encoding = r.encoding
        if encoding is None:
            # detect encoding
            encoding = chardet.detect(contents)['encoding']
        try:
            textcontent = unicode(contents, encoding, errors='replace')
        except (LookupError, TypeError):
            textcontent = unicode(contents, errors='replace')
        print(textcontent)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Python I would like to find the date object for last Wednesday. I
I'm using Python Requests. All works great but today I get this strange error:
I'm trying to download and save an image from the web using python's requests
Is it possible to put multiple requests without breaking the connection using python httplib?.
I am using python sockets to receive web style and soap requests. The code
I am using Python and Requests to request data from Twitter's streaming API .
I am using Python's xmlrpclib to make requests to an xml-rpc service. Is there
Using Python, how does one parse/access files with Linux-specific features, like ~/.mozilla/firefox/*.default ? I've
Hi I'm having trouble posting a text file using the Python Requests Library (
I'm trying to make some requests on the eBay's webservice using python's suds ,

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.