def parse_header(line): """Parse a Content-type like header. Return the main content-type and a dictionary of options. """ plist = line.split(';') print plist key = plist.pop(0).lower().strip() pdict = {} while plist: po = plist.pop(0) p = po.lstrip() i = p.find('=') if i >= 0: name = p[:i].strip().lower() value = p[i+1:].lstrip() if len(value) >= 2 and value[0] == '"': value = value[1:] value = value.replace('\\\\', '\\').replace('\\"', '"') while plist and value.rstrip()[-1]!='"': v = plist.pop(0) v = v.replace('\\\\', '\\').replace('\\"', '"') value += ";" + v value = value[:-1] pdict[name] = value.strip() return key, pdict test = """Content-Disposition: form-data; name="_media"; filename="Demo; 28.07.2006.jpg" """ print test print parse_header(test)