Home > Net >  How can I check to make sure a network connection was successful or not?
How can I check to make sure a network connection was successful or not?

Time:08-12

I am using pysftp to connect to a remote sftp server.

The syntax seems simple:

with pysftp.Connection('hostname', username='me', password='secret') as sftp:

with sftp.cd('/allcode'):
    sftp.put('/pycode/filename')

But how can I check to make sure the pysftp.Connection was successful and the sftp.put actually uploaded the file? I would like to send an email notification if they are not.

Is there a way to do that?

Thanks!

CodePudding user response:

But how can I check to make sure the pysftp.Connection was successful and the sftp.put actually uploaded the file?

Most likely pysftp will throw an exception if there is a problem. You shouldn't check for success before hand. Just send the file and assume it works. Add code to handle exceptions if they occur.

As the saying goes: it is better to ask for forgiveness than to ask for permission.

CodePudding user response:

using urllib you can check your internet connection

import urllib.request
def connect(host='http://google.com'):
    try:
        urllib.request.urlopen(host) #Python 3.x
        return True
    except:
        return False
print( "connected" if connect() else "no internet!" )
  • Related