Python request check response type

Python -- Posted on July 27, 2023

To check if a Python requests response contains a binary file or an HTML page, you can examine the response's content type. The content-type header in the response contains information about the type of content returned by the server. For binary files, the content type may be something like application/pdf, image/jpeg, application/octet-stream, etc. For HTML pages, it is usually text/html.

              
                def is_binary_or_html(response):
    content_type = response.headers.get('content-type', '').lower()
    return 'text/html' in content_type or content_type.startswith('application/')
                  
   
            

Related Posts