Python Forum
network programming - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: network programming (/thread-11323.html)



network programming - karan - Jul-03-2018

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.

Confused


import sys,os

server_ip = '192.168.6.15'
rep = os.system('ping ' + server_ip)
if rep == 0:
print ('server is up ')
else:
print ('server is down')


RE: network programming - Larz60+ - Jul-03-2018

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()) != '127.0.0.1':
            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
>>>



RE: network programming - buran - Jul-03-2018

@Larz60+
if it has to be class, why not just

import socket
 

class Internet:

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