Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
network programming
#1
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')
Reply
#2
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
>>>
Reply
#3
@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'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020