Skip to content

Instantly share code, notes, and snippets.

@kzim44
Created August 12, 2013 20:49
Show Gist options
  • Save kzim44/6215068 to your computer and use it in GitHub Desktop.
Save kzim44/6215068 to your computer and use it in GitHub Desktop.
Post image to rest service with python and urllib2
import urllib2, os
image_path = "png\\01.png"
url = 'http://xx.oo.com/webserviceapi/postfile/'
length = os.path.getsize(image_path)
png_data = open(image_path, "rb")
request = urllib2.Request(url, data=png_data)
request.add_header('Cache-Control', 'no-cache')
request.add_header('Content-Length', '%d' % length)
request.add_header('Content-Type', 'image/png')
res = urllib2.urlopen(request).read().strip()
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment