'Set the boundary parameter in Content-Type to \'boundary\'.
This is subtly different than deleting the Content-Type header and
adding a new one with a new boundary parameter via add_header().  The
main difference is that using the set_boundary() method preserves the
order of the Content-Type header in the original message.
HeaderParseError is raised if the message has no Content-Type header.'

def set_boundary(self, boundary):

    missing = object()
    params = self._get_params_preserve(missing, 'content-type')
    if (params is missing):
        raise errors.HeaderParseError('No    Content-Type    header    found')
    newparams = []
    foundp = False
    for (pk, pv) in params:
        if (pk.lower() == 'boundary'):
            newparams.append(('boundary', ('"%s"' % boundary)))
            foundp = True
        else:
            newparams.append((pk, pv))
    if (not foundp):
        newparams.append(('boundary', ('"%s"' % boundary)))
    newheaders = []
    for (h, v) in self._headers:
        if (h.lower() == 'content-type'):
            parts = []
            for (k, v) in newparams:
                if (v == ''):
                    parts.append(k)
                else:
                    parts.append(('%s=%s' % (k, v)))
            newheaders.append((h, SEMISPACE.join(parts)))
        else:
            newheaders.append((h, v))
    self._headers = newheaders
