Search code examples
pythonmechanize-python

How to get filename from Content-Disposition in headers


I am downloading a file with Mechanize and in response headers there is a string:

Content-Disposition: attachment; filename=myfilename.txt

Is there a quick standard way to get that filename value? What I have in mind now is this:

filename = f[1]['Content-Disposition'].split('; ')[1].replace('filename=', '')

But it looks like a quick'n'dirty solution.


Solution

  • First get the value of the header by using mechanize, then parse the header using the builtin cgi module.

    To demonstrate:

    >>> import mechanize
    >>> browser = mechanize.Browser()
    >>> response = browser.open('http://example.com/your/url')
    >>> info = response.info()
    >>> header = info.getheader('Content-Disposition')
    >>> header
    'attachment; filename=myfilename.txt'
    

    The header value can then be parsed:

    >>> import cgi               
    >>> value, params = cgi.parse_header(header)
    >>> value
    'attachment'
    >>> params
    {'filename': 'myfilename.txt'}
    

    params is a simple dict so params['filename'] is what you need. It doesn't matter whether the filename is wrapped in quotes or not.