0
+from types import ListType, StringType
0
+ """Manage a collection of HTTP response headers"""
0
+ def __init__(self,headers):
0
+ if type(headers) is not ListType:
0
+ raise TypeError("Headers must be a list of name/value tuples")
0
+ self._headers = headers
0
+ """Return the total number of headers, including duplicates."""
0
+ return len(self._headers)
0
+ def __setitem__(self, name, val):
0
+ """Set the value of a header."""
0
+ self._headers.append((name, val))
0
+ def __delitem__(self,name):
0
+ """Delete all occurrences of a header, if present.
0
+ Does *not* raise an exception if the header is missing.
0
+ self._headers[:] = [kv for kv in self._headers if kv[0].lower()<>name]
0
+ def __getitem__(self,name):
0
+ """Get the first header value for 'name'
0
+ Return None if the header is missing instead of raising an exception.
0
+ Note that if the header appeared multiple times, the first exactly which
0
+ occurrance gets returned is undefined. Use getall() to get all
0
+ the values matching a header field name.
0
+ def has_key(self, name):
0
+ """Return true if the message contains the header."""
0
+ return self.get(name) is not None
0
+ __contains__ = has_key
0
+ def get_all(self, name):
0
+ """Return a list of all the values for the named field.
0
+ These will be sorted in the order they appeared in the original header
0
+ list or were added to this instance, and may contain duplicates. Any
0
+ fields deleted and re-inserted are always appended to the header list.
0
+ If no fields exist with the given name, returns an empty list.
0
+ return [kv[1] for kv in self._headers if kv[0].lower()==name]
0
+ def get(self,name,default=None):
0
+ """Get the first header value for 'name', or return 'default'"""
0
+ for k,v in self._headers:
0
+ """Return a list of all the header field names.
0
+ These will be sorted in the order they appeared in the original header
0
+ list, or were added to this instance, and may contain duplicates.
0
+ Any fields deleted and re-inserted are always appended to the header
0
+ return [k for k, v in self._headers]
0
+ """Return a list of all header values.
0
+ These will be sorted in the order they appeared in the original header
0
+ list, or were added to this instance, and may contain duplicates.
0
+ Any fields deleted and re-inserted are always appended to the header
0
+ return [v for k, v in self._headers]
0
+ """Get all the header fields and values.
0
+ These will be sorted in the order they were in the original header
0
+ list, or were added to this instance, and may contain duplicates.
0
+ Any fields deleted and re-inserted are always appended to the header
0
+ return self._headers[:]
0
+ return "Headers(%s)" % `self._headers`
0
+ """str() returns the formatted headers, complete with end line,
0
+ suitable for direct HTTP transmission."""
0
+ return '\r\n'.join(["%s: %s" % kv for kv in self._headers]+['',''])
0
+ def setdefault(self,name,value):
0
+ """Return first matching header value for 'name', or 'value'
0
+ If there is no header named 'name', add a new header with name 'name'
0
+ result = self.get(name)
0
+ self._headers.append((name,value))
0
+ def add_header(self, _name, _value, **_params):
0
+ """Extended header setting.
0
+ _name is the header field to add. keyword arguments can be used to set
0
+ additional parameters for the header field, with underscores converted
0
+ to dashes. Normally the parameter will be added as key="value" unless
0
+ value is None, in which case only the key will be added.
0
+ h.add_header('content-disposition', 'attachment', filename='bud.gif')
0
+ Note that unlike the corresponding 'email.Message' method, this does
0
+ *not* handle '(charset, language, value)' tuples: all values must be
0
+ if _value is not None:
0
+ for k, v in _params.items():
0
+ parts.append(k.replace('_', '-'))
0
+ parts.append(_formatparam(k.replace('_', '-'), v))
0
+ self._headers.append((_name, "; ".join(parts)))
0
\ No newline at end of file
Comments
No one has commented yet.