The shell command curl
and wget
can be called (using os.system
or subprocess.run
)
to download files from internet.
You can also download files using Python modules directly of course.
In [18]:
url = "http://www.legendu.net/media/download_code_server.py"
urllib.request.urlretrieve¶
urllib.request.urlretrieve
can be used to download a file from the internet to local.
For more details,
please refer to Hands on the urllib Module in Python.
In [14]:
import urllib.request
file, http_msg = urllib.request.urlretrieve(
"http://www.legendu.net/media/download_code_server.py",
"/tmp/download_code_server.py",
)
In [15]:
file
Out[15]:
In [9]:
!ls /tmp/download_code_server.py
In [16]:
http_msg
Out[16]:
In [17]:
http_msg.as_string()
Out[17]:
requests¶
Notice that you must open the file to write into with the mode wb
.
In [20]:
import requests
import shutil
resp = requests.get(url, stream=True)
if not resp.ok:
sys.exit("Network issue!")
with open("/tmp/download_code_server_2.py", "wb") as fout:
shutil.copyfileobj(resp.raw, fout)
In [21]:
!ls /tmp/download_code_server_2.py
In [22]:
!cat /tmp/download_code_server_2.py
wget¶
There is no option to overwrite an existing file currently.
However, this can be achieved by renaming/moving the downloaded file (using shutil
).
In [25]:
import wget
wget.download(url, out="/tmp/download_code_server_3.py")
Out[25]:
In [26]:
import wget
wget.download(url, out="/tmp/download_code_server_3.py", bar=wget.bar_adaptive)
Out[26]:
Configure proxy for the Python module wget.
In [ ]:
import socket
import socks
socks.set_default_proxy(socks.SOCKS5, "localhost")
socket.socket = socks.socksocket
pycurl¶
In [29]:
import pycurl
with open("/tmp/download_code_server_4.py", "wb") as fout:
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEDATA, fout)
c.perform()
c.close()
In [ ]: