cardinal_pythonlib.network
Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).
This file is part of cardinal_pythonlib.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Network support functions.
NOTES:
pingrequires root authority to create ICMP sockets in Linuxthe
/bin/pingcommand doesn’t need prior root authority (because it has the setuid bit set)For Linux, it’s therefore best to use the system
ping.
https://stackoverflow.com/questions/2953462/pinging-servers-in-python https://stackoverflow.com/questions/316866/ping-a-site-in-python
Note that if you want a sub-second timeout, things get trickier. One option is
fping.
- cardinal_pythonlib.network.download(url: str, filename: str, skip_cert_verify: bool = True, headers: Dict[str, str] | None = None) None[source]
Downloads a URL to a file.
- cardinal_pythonlib.network.gen_binary_files_from_urls(urls: Iterable[str], on_disk: bool = False, show_info: bool = True, skip_cert_verify: bool = True, headers: Dict[str, str] | None = None) Generator[BinaryIO, None, None][source]
Generate binary files from a series of URLs (one per URL).
- Parameters:
urls¶ – iterable of URLs
on_disk¶ – if
True, yields files that are on disk (permitting random access); ifFalse, yields in-memory files (which will not permit random access)show_info¶ – show progress to the log?
skip_cert_verify¶ – skip SSL certificate check?
headers¶ – request headers (if not specified, a default will be used that mimics Mozilla 5.0 to avoid certain HTTP 403 errors)
- Yields:
files, each of type
BinaryIO