MCPcopy
hub / github.com/PySimpleGUI/PySimpleGUI / net_download_file

Function net_download_file

PySimpleGUI/PySimpleGUI.py:23481–23507  ·  view source on GitHub ↗

Download a file located at a URL on the network :param url: The address of the file :type url: str :param encoding: Encoding data is in. If binary set to '' :type encoding: str :param raise_exception

(url, encoding='utf-8', raise_exception_on_error=False, specific_version=None)

Source from the content-addressed store, hash-verified

23479UA_FOR_URLLIB = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
23480
23481def net_download_file(url, encoding='utf-8', raise_exception_on_error=False, specific_version=None):
23482 """
23483 Download a file located at a URL on the network
23484 :param url: The address of the file
23485 :type url: str
23486 :param encoding: Encoding data is in. If binary set to ''
23487 :type encoding: str
23488 :param raise_exception_on_error: If True, pass back error by raising an exception
23489 :type raise_exception_on_error: bool
23490 :param specific_version: If want a secific version number, use to specify
23491 :type specific_version: str
23492 :return: The file contents. Returns None if an error happened
23493 :rtype: None | str | bytes
23494 """
23495
23496 try:
23497 req = urllib.request.Request(url, headers={'User-Agent': UA_FOR_URLLIB})
23498 res = urllib.request.urlopen(req)
23499 data = res.read()
23500 if encoding:
23501 data = data.decode(encoding)
23502 except Exception as e:
23503 if raise_exception_on_error:
23504 raise e
23505 return None # error
23506
23507 return data
23508
23509
23510def net_download_file_binary(url):

Callers 3

net_download_file_binaryFunction · 0.85
upgrade_PySimpleGUI_guiFunction · 0.85

Calls 1

readMethod · 0.45

Tested by

no test coverage detected