jdagomes
jdagomes

Reputation: 31

Python script to download Java JDK Windows

I'm trying to download Java JDK from Oracle website with a Python script. The code is this:

import urllib2

def download(cookie, license, url, filename):
    print url
    print filename
    opener = urllib2.build_opener()
    opener.addheaders.append((cookie, license))
    f = opener.open(url)
    with open(filename, 'w+') as save:
        save.write(f.read())

cookie = 'Cookie'
license = 'oraclelicense=accept-securebackup-cookie'
url = 'http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-windows-x64.exe'
filename = 'jdk-8u131-windows-x64.exe'

download(cookie, license, url, filename)

The code downloads a file, but the downloaded file with this code has 203 576KB, when the original file should have 202 784KB and when I try to run it it says that is not possible to run this file on my computer.

With the same code, if I change the url and filename variables to the Linux version like this:

url = 'http://download.oracle.com/otn-pub/java/jdk/8-b132/jdk-8-linux-x64.tar.gz'
filename = 'jdk-8-linux-x64.tar.gz'

It works and downloads the file without problems. What can I change in the code to make it work in Windows?

Upvotes: 1

Views: 1634

Answers (1)

jdagomes
jdagomes

Reputation: 31

I figured it out. I wasn't saving the file in binary mode so just need to change the mode from 'w+' to 'wb+' like this:

with open(filename, 'wb+') as save:
        save.write(f.read())

Upvotes: 2

Related Questions