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)
| 23479 | UA_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 | |
| 23481 | def 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 | |
| 23510 | def net_download_file_binary(url): |
no test coverage detected