Skip to content

Commit 347185a

Browse files
authored
Properly import URLError and urlopen in both Python 2 and Python 3
flake8 testing of https://github.com/geekcomputers/Python on Python 3.7.1 $ __flake8 . --count --select=E9,F63,F72,F82 --show-source --statistics__ ``` ./check_internet_con.py:12:9: F821 undefined name 'urllib2' urllib2.urlopen("http://google.com", timeout=2) ^ ./check_internet_con.py:15:12: F821 undefined name 'urllib2' except urllib2.URLError as E: ^ ```
1 parent f44b26c commit 347185a

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

check_internet_con.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
try:
44
# For Python 3.0 and later
55
from urllib.request import urlopen
6+
from urllib.error import URLError
67
except ImportError:
78
# Fall back to Python 2's urllib2
8-
from urllib2 import urlopen
9+
from urllib2 import URLError, urlopen
910

1011
def checkInternetConnectivity():
1112
try:
12-
urllib2.urlopen("http://google.com", timeout=2)
13+
urlopen("http://google.com", timeout=2)
1314
print("Working connection")
14-
1515
except urllib2.URLError as E:
1616
print("Connection error:%s" % E.reason)
1717

0 commit comments

Comments
 (0)