Python Forum

Full Version: network programming
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Folks,
I am trying to use the following script to check if the IP is active or not, but the result shows active irrespective of the actual status.


import sys,os

server_ip = ''
rep = os.system('ping ' + server_ip)
if rep == 0:
print ('server is up ')
print ('server is down')
this will work most of the time:
import socket

class CheckInternet:
    def __init__(self):
        self.internet_available = False

    def check_availability(self):
        self.internet_available = False
        if socket.gethostbyname(socket.gethostname()) != '':
            self.internet_available = True
        return self.internet_available
to use:
λ python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import CheckInternet
>>> ci = CheckInternet.CheckInternet()
>>> if not ci.check_availability():
...    print('Pleas activate internet')
... else:
...    print('Internet connection found')
Internet connection found
if it has to be class, why not just

import socket

class Internet:

    def available(self):
        return socket.gethostbyname(socket.gethostname()) != ''