-2

I have a problem because I need to download a file using python but I cannot use the libraries urllib, urllib2 and urllib3 neither request

If someone can help me I thanks him a lot

Dark_John
  • 1
  • 1

3 Answers3

2

you can use subprocess module and curl command

import subprocess
result = subprocess.run(['curl', 'https://www.google.com'], stdout=subprocess.PIPE)
# do with result
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Roushan
  • 4,074
  • 3
  • 21
  • 38
0

You need to do wget in terminal to get the package. Why can't you use these!? (I can't ugh comment)

Try using requests to get the data of the file and write it a path if you can't use urllib's package.

To download files with requests use this example from here

def download_file(url):
    local_filename = url.split('/')[-1]
    # NOTE the stream=True parameter
    r = requests.get(url, stream=True)
    with open(local_filename, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024): 
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)
                #f.flush() commented by recommendation from J.F.Sebastian
    return local_filename

Here's the aiohttp example for async functions:

import aiohttp

async def download_file(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            with open(filename, 'wb') as fd:
                while True:
                    chunk = await resp.content.read(chunk_size)
                    if not chunk:
                        break
                    fd.write(chunk)

url = "URL"
await download_file(url)

I recommend using aiohttp if you don't can't use requests.

NekoTony
  • 54
  • 8
0

Thanks to all, finally I could solve my problem I give you the solution that I used

import * from socket
    s = socket(AF_INET, SOCK_STREAM)
    s.connect((url, port))
    file_mess="GET /" + step2[:5] + " HTTP/1.1\r\n\r\n"
    s.send((file_mess).encode('utf-8'))
    print(s.recv(1024).decode('utf-8'))
    print(s.recv(1024).decode('utf-8'))

I used the second print for obtaining all the data, because with the first I only get a 200 OK

Dark_John
  • 1
  • 1