简体   繁体   中英

Downloading file from redirection link python

I am trying to make a simple program that will help with the confusing part of rooting.

I need to download the file from tiny.cc/latestmagisk

I am using this python code

import request
url = tiny.cc/latestmagisk

r = request.get(url)
r.content

The content it returns is the usual 403 Forbidden for nginx

I need this to work with the shortened URL is there anyway to make that happen?

Contrary to the other answer, you really should use requests for this as requests has better support for redirects.

For getting a page through a redirect from requests:

r=requests.get(url, allow_redirects=True)

For downloading files through redirects:

r = requests.get(url, allow_redirects=True, stream=True)
with open(filename, 'wb') as f:
    for chunk in r.iter_content(chunk_size=1024): 
        if chunk: f.write(chunk)

However, in this case, either tiny.cc or XDA does not allow a simple requests.get; the 403 forbidden is likely due to the User-Agent or other intrinsic header as this method works well with bit.ly and other shortlink generators. You may need to fake headers.

its's not necessary to import request lib
all you need to do is import ssl, urllib and pass ssl._create_unverified_context() as context to the server while you're sendig a request!
your code should be look like this:

import ssl, urllib

certcontext = ssl._create_unverified_context()
f = open('image.jpg','wb') #creating placeholder

#creating image from url and saving it as `image.jpg`!
f.write(urllib.urlopen("https://i.stack.imgur.com/IKh7E.png", context=certcontext).read())

f.close()


note: it will save the image as image.jpg file ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM