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.

5
  • Can you please post what code you've tried so far? Commented Aug 29, 2020 at 18:16
  • 1
    You make a request and handle the exception once it is raised. Commented Aug 29, 2020 at 18:18
  • Does this answer your question? Checking network connection Commented Aug 29, 2020 at 18:25
  • No as I don't know about urllib2 and I am using urllib3 Commented Aug 29, 2020 at 18:28
  • It works with any version of urllib. Commented Aug 29, 2020 at 18:29

2 Answers 2

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)) 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.