I am python 3.5 and using urllib3 and I have seen various examples but they were of urllib2 that's why I asked this question ,I have a bit of code which need internet but if user is not connected I want to show them a warning that device is not connected to internet so how can I do this.
Asked
Active
Viewed 157 times
1
-
Can you please post what code you've tried so far? – kingkupps Aug 29 '20 at 18:16
-
1You make a request and handle the exception once it is raised. – Klaus D. Aug 29 '20 at 18:18
-
Does this answer your question? [Checking network connection](https://stackoverflow.com/questions/3764291/checking-network-connection) – Mario Ishac Aug 29 '20 at 18:25
-
No as I don't know about urllib2 and I am using urllib3 – Saksham Kushwaha Aug 29 '20 at 18:28
-
It works with any version of `urllib`. – Mario Ishac Aug 29 '20 at 18:29
2 Answers
2
You could do something like this where you check for connection to a site.
import urllib.request
import urllib.parse
try:
x = urllib.request.urlopen('https://www.google.com')
except Exception as e:
print(str(e))

Mario Ishac
- 5,060
- 3
- 21
- 52

Arundeep Chohan
- 9,779
- 5
- 15
- 32
0
Ive not used urllib before but you can try something like:
try:
#you put your code here
#and it will raise an exception if
#network connection is not available
#so you catch that
except:
#your warning code

Zephyr
- 11,891
- 53
- 45
- 80