Python Forum

Full Version: Posting zip file on HTTP network using Python2.7
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

We need to post a zip file on HTTP using Python 2.7.

We are using the below code which throwing exception:

post_data = open(zipPath,u'rb')
request = urllib2.Request(url,post_data.read().encode(u'utf-8'))
request.add_header(u"Content-Type", u"application/zip")
Please help.

Thanks and Regards
We want to send a POST request, and in data we want to send .zip file
post_data = open(zipFilePath, 'rb')
readData = post_data.read()
headers = { 'Content-Type' : 'application/zip', 'Accept' : '*/*' }
url = "someURL"

#Python3.0
req = urllib.request.Request(url, post_data.read(), headers)
res = urllib.request.urlopen(req)

#Python2.7
req = urllib2.Request(url, post_data.read(), headers)
res = urllib2.urlopen(req)
We have observed that the value of 'readData' for the same zip file is different for Python 3 and Python 2.7
We want to read the zip file in Python 2.7 in the same exact way it's been read in Python 3.0, so that the zip file can be send in POST request using 'urllib2.urlopen()' function.

I have tried adding python tags but it seems I am doing something wrong. Sorry for inconvenience. Please help to understand the issue.

Thanks for your help.