Parsing a URL with urllib to get the filename

When downloading content from a URL, we often want to save it in a file.  Often it is good enough to save the file in a file with a name found in the URL.  But the URL consists of a number of fragments, so how can we find the actual filename from the URL, especially where there are often many parameters after the file name?