Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add has_internet function to test_helpers #38

Merged
merged 1 commit into from
Jan 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions testr/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,30 @@ def on_head_network():

def is_32_bit():
return sys.maxsize <= 2 ** 32


def has_internet(host="8.8.8.8", port=53, timeout=3):
"""Return True if internet is available by trying to reach ``host``.

Adapted from https://stackoverflow.com/questions/3764291

Parameters
----------
host : str
Host IP to reach (default=8.8.8.8 (google-public-dns-a.google.com))
port : int
Port to use (default=53)
timeout : int, float
Timeout in seconds (default=3)

Returns
-------
has_internet : bool
"""
import socket
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except socket.error:
return False