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:

  • ping requires root authority to create ICMP sockets in Linux

  • the /bin/ping command 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.

Parameters:
  • 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)

cardinal_pythonlib.network.gen_binary_files_from_urls(urls: Iterable[str], on_disk: bool = False, show_info: bool = True) 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); if False, yields in-memory files (which will not permit random access)

  • show_info – show progress to the log?

Yields:

files, each of type BinaryIO

cardinal_pythonlib.network.ping(hostname: str, timeout_s: int = 5) bool[source]

Pings a host, using OS tools.

Parameters:
  • hostname – host name or IP address

  • timeout_s – timeout in seconds

Returns:

was the ping successful?