首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python ICMP Pinger

Python ICMP Pinger
EN

Stack Overflow用户
提问于 2017-12-06 11:14:20
回答 1查看 1.6K关注 0票数 1

我试图为类创建一个ICMP Pinger,但是当我试图编译它时,我得到了这个错误。我不确定还能做什么,大部分代码都是我课本上的框架代码,所以我不完全确定哪里出了问题。

当我编译这段代码时

代码语言:javascript
复制
from socket import *
import os
import sys
import struct
import time
import select
import binascii

ICMP_ECHO_REQUEST = 8

def checksum(str):
    csum = 0
    countTo = (len(str) / 2) * 2

    count = 0
    while count < countTo:
        thisVal = ord(str(str[count+1])) * 256 + ord(str(str[count]))
        csum = csum + thisVal
        csum = csum & 0xffffffff
        count = count + 2

    if countTo < len(str):
        csum = csum + ord(str[leb(str) - 1])
        csum = csum & 0xffffffff

    csum = (csum >> 16) + (csum & 0xffff)
    csum = csum + (csum >> 16)
    answer = ~csum
    answer = answer & 0xffff
    andswer = answer >> 8 | (answer << 8 & 0xff00)
    return answer

def recieveOnePing(mySocket, ID, timeout, destAddr):
    timeLeft = timeout

    while 1:
        startedSelect = time.time()
        whatReady = select.select([mySocket], [], [], timeLeft)
        howLongInSelect = (time.time() - startedSelect)
        if whatReady[0] == []: # Timeout
            return "Request timed out."
        timeRecieved = time.time()
        recPacket, addr = mySocket.recvfrom(1024)

        icmph = recPacket[20:28]
        type, code, checksum, pID, sq = struct.unpack("bbHHh", icmph)

        print ("The header received in the ICMP reply is ",type, code, checksum, pID, sq)
        if pID == ID:
            bytesinDbl = struct.calcsize("d")
            timeSent = struct.unpack("d", recPacket[28:28 + bytesinDbl])[0]
            rtt=timeReceived - timeSent

        print ("RTT is : ")
        return rtt

        timeLeft = timeLeft - howLongInSelect
        if timeLeft <= 0:
            return "Request timed out"

def sendOnePing(mySocket, destAddr, ID):
    myChecksum = 0
    header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0 , myChecksum, ID, 1)
    data = struct.pack("d", time.time())
    myChecksum = checksum(header + data)

    if sys.platform == 'darwin':
        myChecksum = socket.htons(myChecksum) & 0xffff
    else:
        myChecksum = socket.htons(myChecksum)

    header = stuct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, myChecksum, ID, 1)
    packet = header + data

    mySocket.sendto(packet, (destAddr, 1))

def doOnePing(destAddr, timeout):
    icmp = getprotobyname("icmp")

    mySocket = socket(AF_INET, SOCK_STREAM)

    myId = os.getpid() & 0xFFFF
    sendOnePing(mySocket, destAddr, myID)
    delay = recieveOnePing(mySocket, myID, timeout, destAddr)

    mySocket.close()
    return delay

def ping(host, timeout=1):
    dest = gethostbyname(host)
    print ("Pinging" + dest + "using Python:")
    print ("")
    while 1:
        delay = doOnePing(dest, timeout)
        print(delay)
        time.sleep(1)
    return delay

ping("www.google.com")

我得到了这个错误

代码语言:javascript
复制
File "ICMPPinger.py", line 17, in checksum
    thisVal = ord(str(str[count+1])) * 256 + ord(str(str[count]))
TypeError: 'bytes' object is not callable

我应该重命名我的变量吗?我不确定这行应该做什么,以及我什么时候把它当作

代码语言:javascript
复制
thisVal = ord(str[count+1]) * 256 + ord(str[count]))

我收到错误:文件"ICMPPinger.py",第17行,校验和thisVal = ord(strcount+1) * 256 + ord(strcount) TypeError: ord()需要长度为1的字符串,但找到的是整型

EN

回答 1

Stack Overflow用户

发布于 2017-12-06 11:22:27

在def doOnePing()中,您定义了名为myId的变量,但是传递的变量名为myID,这就是为什么会出现未定义变量错误的原因。因此,在定义变量时,将myId替换为myID,如下所示:

代码语言:javascript
复制
myID = os.getpid() & 0xFFFF
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47666223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档