This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sage.doctest.external.has_internet: Refactor through new Feature
- Loading branch information
Matthias Koeppe
committed
Oct 7, 2021
1 parent
52915b6
commit 1c6335a
Showing
2 changed files
with
51 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from . import Feature, FeatureTestResult | ||
|
||
|
||
class Internet(Feature): | ||
r""" | ||
A feature describing if Internet is available. | ||
Failure of connecting to the site "https://www.sagemath.org" within a second | ||
is regarded as internet being not available. | ||
EXAMPLES:: | ||
sage: from sage.features.internet import Internet | ||
sage: Internet() | ||
Feature('internet') | ||
""" | ||
|
||
def __init__(self): | ||
r""" | ||
TESTS:: | ||
sage: from sage.features.internet import Internet | ||
sage: Internet() is Internet() | ||
True | ||
""" | ||
Feature.__init__(self, 'internet') | ||
|
||
def _is_present(self): | ||
r""" | ||
Test whether Internet is available. | ||
EXAMPLES:: | ||
sage: from sage.features.internet import Internet | ||
sage: Internet().is_present() # random, optional -- internet | ||
FeatureTestResult('internet', True) | ||
""" | ||
import urllib.error | ||
from urllib.request import Request, urlopen | ||
from ssl import SSLContext | ||
|
||
req = Request("https://www.sagemath.org", headers={"User-Agent": "sage-doctest"}) | ||
try: | ||
urlopen(req, timeout=1, context=SSLContext()) | ||
return FeatureTestResult(self, True) | ||
except urllib.error.URLError: | ||
return FeatureTestResult(self, False) |