python类实例方法改为类方法

1.python类的类方法不能调用,类中的实例方法,如需调用把调用的方法也改为类方法

#conding=utf-8
import socket,struct,time,select

class CheckLive(object):
    ICMP_ECHO = 8
    @classmethod
    def checksum(cls, source_string):
        sum = 0
        count_to = int(len(source_string) / 2) * 2
        for count in xrange(0, count_to, 2):
            this = ord(source_string[count + 1]) * 256 + ord(source_string[count])
            sum = sum + this
            sum = sum & 0xffffffff
        if count_to < len(source_string):
            sum = sum + ord(source_string[len(source_string) - 1])
            sum = sum & 0xffffffff
        sum = (sum >> 16) + (sum & 0xffff)
        sum = (sum >> 16) + sum
        ans = ~sum
        return ans & 0Xffff
    @classmethod
    def send_one_ping(cls, my_socket, dest_addr, id, psize):
        dest_addr = socket.gethostbyname(dest_addr)
        psize = psize - 8
        my_checksum = 0
        header = struct.pack("bbHHh", CheckLive.ICMP_ECHO, 0, my_checksum, id, 1)
        bytes = struct.calcsize("d")
        data = (psize - bytes) * "Q"
        data = struct.pack("d", time.time()) + data
        my_checksum = cls.checksum(header + data)
        header = struct.pack('bbHHh', CheckLive.ICMP_ECHO, 0, my_checksum, id, 1)
        package = header + data
        try:
            return my_socket.sendto(package, (dest_addr, 1))
        except:
            return None
    @classmethod
    def receive_one_ping(cls, my_socket, id, timeout):
        time_left = timeout
        while True:
            start_select = time.time()
            events = select.select([my_socket, ], [], [], time_left)
            time_received = time.time()
            if events[0] == []:
                return
            received_packet, addr = my_socket.recvfrom(1024)
            icmpHeader = received_packet[20:28]
            type, code, checksum, package_id, sequence = struct.unpack(
                "bbHHh", icmpHeader
            )
            if package_id == id:
                bytes = struct.calcsize("d")
                time_sent = struct.unpack("d", received_packet[28:28 + bytes])[0]
                return time_received - time_sent
            time_left = time_left - (time_received - start_select)
            if time_left < 0:
                break
    @classmethod
    def ping_ip(cls, ip):
        icmp = socket.getprotobyname('icmp')
        s = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
        cls.send_one_ping(s, ip, 1, 80)
        w = cls.receive_one_ping(s, 1, 10)
        #print w
        return w

if __name__ == '__main__':

    if CheckLive.ping_ip('192.168.215.174'):
        print 'yes'
    else:
        print "no"

2.ping检测的普通函数

#coding=utf-8
import re
import time
import socket
import struct
import select
ICMP_ECHO = 8

def checksum(source_string):
    sum = 0
    count_to = int(len(source_string)/2)*2
    for count in xrange(0, count_to, 2):
        this = ord(source_string[count + 1]) * 256 + ord(source_string[count])
        sum = sum + this
        sum = sum & 0xffffffff 
    if count_to < len(source_string):
        sum = sum + ord(source_string[len(source_string) - 1])
        sum = sum & 0xffffffff
    sum = (sum >> 16) + (sum & 0xffff)
    sum = (sum >> 16) + sum
    ans = ~sum
    return ans & 0Xffff

def send_one_ping(my_socket, dest_addr, id, psize):
    dest_addr  =  socket.gethostbyname(dest_addr)
    psize = psize - 8
    my_checksum = 0
    header = struct.pack("bbHHh", ICMP_ECHO, 0, my_checksum, id, 1)
    bytes = struct.calcsize("d")
    data = (psize - bytes) * "Q"
    data = struct.pack("d", time.time()) + data
    my_checksum = checksum(header + data)
    header = struct.pack('bbHHh', ICMP_ECHO, 0, my_checksum, id, 1)
    package = header + data
    try:
        return my_socket.sendto(package, (dest_addr,1))
    except:
        return None


def receive_one_ping(my_socket, id, timeout):
    time_left = timeout
    while True:
        start_select = time.time()
        events = select.select([my_socket, ],[],[],time_left)
        time_received = time.time()
        if events[0] == []:
            return 
        received_packet, addr = my_socket.recvfrom(1024)
        icmpHeader = received_packet[20:28]
        type, code, checksum, package_id, sequence = struct.unpack(
            "bbHHh", icmpHeader
        )
        if package_id == id:
            bytes = struct.calcsize("d")
            time_sent = struct.unpack("d", received_packet[28:28 + bytes])[0]
            return time_received - time_sent
        time_left = time_left - (time_received - start_select)
        if time_left < 0:
            break


def change_name(s):
    """
        s is a str
    """
    s = re.sub('-','_',s)
    return s

def ping_ok(d):
    ip = d.get('address','abc')
    icmp = socket.getprotobyname('icmp')
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
    if not send_one_ping(s, ip, 1, 1024):
        return False
    return True if receive_one_ping(s, 1, 5) else False

3.建ping检测的普通函数封装为类,类方法属于实例方法

import socket,struct,time,select

#check kvm alive
class check_alive(object):
    ICMP_ECHO = 8
    def checksum(self, source_string):
        sum = 0
        count_to = int(len(source_string) / 2) * 2
        for count in xrange(0, count_to, 2):
            this = ord(source_string[count + 1]) * 256 + ord(source_string[count])
            sum = sum + this
            sum = sum & 0xffffffff
        if count_to < len(source_string):
            sum = sum + ord(source_string[len(source_string) - 1])
            sum = sum & 0xffffffff
        sum = (sum >> 16) + (sum & 0xffff)
        sum = (sum >> 16) + sum
        ans = ~sum
        return ans & 0Xffff

    def send_one_ping(self, my_socket, dest_addr, id, psize):
        dest_addr = socket.gethostbyname(dest_addr)
        psize = psize - 8
        my_checksum = 0
        header = struct.pack("bbHHh", check_alive.ICMP_ECHO, 0, my_checksum, id, 1)
        bytes = struct.calcsize("d")
        data = (psize - bytes) * "Q"
        data = struct.pack("d", time.time()) + data
        my_checksum = self.checksum(header + data)
        header = struct.pack('bbHHh', check_alive.ICMP_ECHO, 0, my_checksum, id, 1)
        package = header + data
        try:
            return my_socket.sendto(package, (dest_addr, 1))
        except:
            return None

    def receive_one_ping(self, my_socket, id, timeout):
        time_left = timeout
        while True:
            start_select = time.time()
            events = select.select([my_socket, ], [], [], time_left)
            time_received = time.time()
            if events[0] == []:
                return
            received_packet, addr = my_socket.recvfrom(1024)
            icmpHeader = received_packet[20:28]
            type, code, checksum, package_id, sequence = struct.unpack(
                "bbHHh", icmpHeader
            )
            if package_id == id:
                bytes = struct.calcsize("d")
                time_sent = struct.unpack("d", received_packet[28:28 + bytes])[0]
                return time_received - time_sent
            time_left = time_left - (time_received - start_select)
            if time_left < 0:
                break

    def ping_ip(self, ip):
        icmp = socket.getprotobyname('icmp')
        s = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
        self.send_one_ping(s, ip, 1, 80)
        w = self.receive_one_ping(s, 1, 10)
        #print w
        return w

猜你喜欢

转载自blog.csdn.net/jackliu16/article/details/81176819