Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 0.64 KB  |  hits: 123  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. python: HTTP PUT with unencoded binary data
  2. Content-Type: application/octet-stream
  3. Content-Length: (whatever my binary data length is)
  4.        
  5. import urllib2
  6.  
  7. data = b'binary-data'
  8. r = urllib2.Request('http://example.net/put', data,
  9.                     {'Content-Type': 'application/octet-stream'})
  10. r.get_method = lambda: 'PUT'
  11. urllib2.urlopen(r)
  12.        
  13. PUT /put HTTP/1.1
  14. Accept-Encoding: identity
  15. Content-Length: 11
  16. Host: example.net
  17. Content-Type: application/octet-stream
  18. Connection: close
  19. User-Agent: Python-urllib/2.7
  20.  
  21. binary-data
  22.        
  23. # headers contains `{'Content-Type': 'application/octet-stream'}`
  24. r = urllib2.Request(url.encode('utf-8'), data, headers)