Message129348
Both httplib.HTTPMessage and email.message.Message classes[1] implements methods for RFC822 headers parsing. Unfortunately, they have different implementations and they do not provide the same level of functionality.
One example that is bugging me is that:
* httplib.HTTPMessage is missing the get_filename method present in email.message.Message, that allows you to easily retrieve the filename from a 'Content-disposition: attachment; filename="fghi.xyz"' header;
* httplib.HTTPMessage has getparam, getplist and parseplist methods but AFAIK, they are not and cannot be used outside of the 'content-type' header parsing;
* email.message.Message has a generic get_param method to parse any RFC822 header with parameters, such as 'content-disposition' or 'content-type'.
The workaround I'm using is to decorate an httplib.HTTPMessage with the missing methods from email.message.Message:
def monkeypatch_http_message(obj):
""" Decorate an httplib.HTTPMessage instance's class
with the RFC822 header parameters parsing methods
from email.message.Message. (thanks to ncoghlan)
"""
import httplib
assert isinstance(obj, httplib.HTTPMessage)
cls = obj.__class__
from email import utils
from email.message import (
_parseparam,
_unquotevalue,
Message
)
funcnames = (
'_get_params_preserve',
'get_params',
'get_param',
'get_filename'
)
for funcname in funcnames:
cls.__dict__[funcname] = Message.__dict__[funcname]
So I can do:
import mechanize
from some.module import monkeypatch_http_message
browser = mechanize.Browser()
# in that form, browser.retrieve returns a temporary filename
# and an httplib.HTTPMessage instance
(tmp_filename, headers) = browser.retrieve(someurl)
# monkeypatch the httplib.HTTPMessage instance
monkeypatch_http_message(headers)
# yeah... my original filename, finally
filename = headers.get_filename() |
|
Date |
User |
Action |
Args |
2011-02-25 10:37:52 | jrjsmrtn | set | recipients:
+ jrjsmrtn |
2011-02-25 10:37:52 | jrjsmrtn | set | messageid: <1298630272.85.0.58708707749.issue11316@psf.upfronthosting.co.za> |
2011-02-25 10:37:52 | jrjsmrtn | link | issue11316 messages |
2011-02-25 10:37:51 | jrjsmrtn | create | |
|