title: python: HTTP PUT with binary data
body: <p>So I adapted urllib2 as suggested by <a href="https://stackoverflow.com/questions/4511598/how-to-make-http-delete-method-using-urllib2">answers to another question</a>:</p>
  
  <pre><code>class HttpRequest(urllib2.Request):
    def __init__(self, *args, **kwargs):
      self._method = kwargs.pop('method', 'GET')
      urllib2.Request.__init__(self, *args, **kwargs)
    def get_method(self):
      return self._method
  </code></pre>
  
  <p>and it works nicely for PUT with JSON:</p>
  
  <pre><code>req = HttpRequest(url=url, method='PUT', 
      data=json.dumps(metadata))
  response = urllib2.urlopen(req)
  </code></pre>
  
  <p>but it fails with <code>data=</code> binary data (partial stacktrace below):</p>
  
  <pre><code>  File "c:\appl\python\2.7.2\lib\urllib2.py", line 126, in urlopen
      return _opener.open(url, data, timeout)
    File "c:\appl\python\2.7.2\lib\urllib2.py", line 394, in open
      response = self._open(req, data)
    File "c:\appl\python\2.7.2\lib\urllib2.py", line 412, in _open
      '_open', req)
    File "c:\appl\python\2.7.2\lib\urllib2.py", line 372, in _call_chain
      result = func(*args)
    File "c:\appl\python\2.7.2\lib\urllib2.py", line 1199, in http_open
      return self.do_open(httplib.HTTPConnection, req)
    File "c:\appl\python\2.7.2\lib\urllib2.py", line 1168, in do_open
      h.request(req.get_method(), req.get_selector(), req.data, headers)
    File "c:\appl\python\2.7.2\lib\httplib.py", line 955, in request
      self._send_request(method, url, body, headers)
    File "c:\appl\python\2.7.2\lib\httplib.py", line 989, in _send_request
      self.endheaders(body)
    File "c:\appl\python\2.7.2\lib\httplib.py", line 951, in endheaders
      self._send_output(message_body)
    File "c:\appl\python\2.7.2\lib\httplib.py", line 809, in _send_output
      msg += message_body
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 10: ordinal
   not in range(128)
  </code></pre>
  
  <p>Is there a way I can fix this?</p>