Original code copyright (C) 2009-2022 Rudolf Cardinal (firstname.lastname@example.org).
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.
pingrequires root authority to create ICMP sockets in Linux
/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
- Note that if you want a sub-second timeout, things get trickier.
One option is
download(url: str, filename: str, skip_cert_verify: bool = True, headers: Dict[str, str] = None) → None¶
Downloads a URL to a file.
- url – URL to download from
- filename – file to save to
- 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)
gen_binary_files_from_urls(urls: Iterable[str], on_disk: bool = False, show_info: bool = True) → Generator[BinaryIO, None, None]¶
Generate binary files from a series of URLs (one per URL).
- urls – iterable of URLs
- on_disk – if
True, yields files that are on disk (permitting random access); if
False, yields in-memory files (which will not permit random access)
- show_info – show progress to the log?
files, each of type
ping(hostname: str, timeout_s: int = 5) → bool¶
Pings a host, using OS tools.
- hostname – host name or IP address
- timeout_s – timeout in seconds
was the ping successful?