1

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.

Saksham Kushwaha
  • 274
  • 1
  • 18

2 Answers2

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