4

How can I check if there is still connection from a specific ip address using python.

Netorica
  • 18,523
  • 17
  • 73
  • 108
Nethan
  • 251
  • 1
  • 6
  • 18
  • 1
    I belive you mean *to* a specific IP address. that is, a [ping](http://stackoverflow.com/questions/316866/ping-a-site-in-python)? – Elazar May 29 '13 at 08:17
  • Yes Elazar,to a specific address and yes it's a ping. If I ping this ip address and if there is connection,it will return true, if not, it will return false. – Nethan May 29 '13 at 09:39
  • Then look at the link in my comment. The answer is there. – Elazar May 29 '13 at 09:58

4 Answers4

4

ping the ip address

import os
#192.168.1.10 is the ip address
ret = os.system("ping -o -c 3 -W 3000 192.168.1.10")
if ret != 0:
    print "pc still alive"

well in any case you really want to check for availability of incoming connection on the PC you are trying to connect you need to make a program that will receive the connection which is already out of the question.

jwfearn
  • 28,781
  • 28
  • 95
  • 122
Netorica
  • 18,523
  • 17
  • 73
  • 108
3

As far as I understood the OP is looking for active connection FROM certain ip, meaning he wants to check locally if there is active connection exists. It looks like something along lines of netstat to me. There are several options:

  1. You can use psutils as demonstrated in this post. You will want to cycle the active processes and query active connections.

  2. You could use netstat.py - a clone of netstat by Jay Loden, Giampaolo Rodola' to do the job for you.

Added:

You can do something like that:

import psutil

def remote_ips():
    '''
    Returns the list of IPs for current active connections

    '''

    remote_ips = []

    for process in psutil.process_iter():
        try:
            connections = process.get_connections(kind='inet')
        except psutil.AccessDenied or psutil.NoSuchProcess:
            pass
        else:
            for connection in connections:
                if connection.remote_address and connection.remote_address[0] not in remote_ips:
                    remote_ips.append(connection.remote_address[0])

    return remote_ips

def remote_ip_present(ip):
    return ip in remote_ips()

This is how it works:

>>>remote_ips()
['192.168.1.50', '192.168.1.15', '192.168.1.52', '198.252.206.16', '198.252.206.17'] 
>>>remote_ip_present('192.168.1.52')
True
>>>remote_ip_present('10.1.1.1')
False
jwfearn
  • 28,781
  • 28
  • 95
  • 122
PSS
  • 5,561
  • 5
  • 28
  • 30
2

You can use socket library:

import socket

try:
    socket.gethostbyaddr(your_ip_adrress)
except socket.herror:
    print u"Unknown host"
kadi
  • 184
  • 8
  • 2
    Hi kadi, I tried this but is says Unknown Host None even thought there is a connection. – Nethan May 29 '13 at 09:21
  • Hi Nethan, If Host is not reachable it means that there is no connection opened on this address. – kadi May 29 '13 at 12:44
  • 4
    @kadi `gethostbyaddr` doesn't say anything about connectivity (in any sense). It merely returns the name associated with the IP address. It's perfectly valid for a name to be returned for a host which is not currently up. Or for no name to be returned for a host that *is* up. – Gil Hamilton Jul 31 '18 at 20:27
0

if you are on Windows:

import os
ret = os.system("ping -n 3 1.1.1.1")
if ret != 0:
     print("Ip address responding")

The -n argument is for how many times to ping it

if you are on LInux:

import os
ret = os.system("ping -c 3 1.1.1.1")
if ret != 0:
     print("Ip address responding")
omboybread
  • 13
  • 5