title: HTTP POST binary files using Python: concise non-pycurl examples?
body: <p>I'm interested in writing a short python script which uploads a short binary file (.wav/.raw audio) via a POST request to a remote server.</p>
  
  <p>I've done this with pycurl, which makes it very simple and results in a concise script; unfortunately it also requires that the end 
  user have pycurl installed, which I can't rely on.  </p>
  
  <p>I've also  seen some examples in other posts which rely only on basic libraries, urllib, urllib2, etc., however these generally seem to be quite verbose, which is also something I'd like to avoid.  </p>
  
  <p>I'm wondering if there are any concise examples which do not require the use of external libraries, and which will be quick and easy for 3rd parties to understand - even if they aren't particularly familiar with python.</p>
  
  <p>What I'm using at present looks like,</p>
  
  <pre><code>
  def upload_wav( wavfile, url=None, **kwargs ):
      """Upload a wav file to the server, return the response."""
  
      class responseCallback:
          """Store the server response."""
          def __init__(self):
              self.contents=''
          def body_callback(self, buf):
              self.contents = self.contents + buf
  
          def decode( self ):
              self.contents = urllib.unquote(self.contents)
              try:
                  self.contents = simplejson.loads(self.contents)
              except:
                  return self.contents
  
      t = responseCallback()
      c = pycurl.Curl()
      c.setopt(c.POST,1)
      c.setopt(c.WRITEFUNCTION, t.body_callback)
      c.setopt(c.URL,url)
      postdict = [
          ('userfile',(c.FORM_FILE,wavfile)),  #wav file to post                                                                                 
          ]
      #If there are extra keyword args add them to the postdict                                                                                  
      for key in kwargs:
          postdict.append( (key,kwargs[key]) )
      c.setopt(c.HTTPPOST,postdict)
      c.setopt(c.VERBOSE,verbose)
      c.perform()
      c.close()
      t.decode()
      return t.contents
  </code></pre>
  
  <p>this isn't exact, but it gives you the general idea.  It works great, it's simple for 3rd parties to understand, <em>but</em> it requires pycurl.</p>