Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python [Errno 56]:插座已连接修复?_Python_Sockets_Python 3.x_Icmp - Fatal编程技术网

Python [Errno 56]:插座已连接修复?

Python [Errno 56]:插座已连接修复?,python,sockets,python-3.x,icmp,Python,Sockets,Python 3.x,Icmp,我正在开发一个ICMP pinger程序,遇到了一个问题。当我运行程序时,我首先在终端中执行sudo su,因此我有根访问权限,因为我们需要使用原始套接字,然后当我运行程序时,我得到这个回溯 sh-3.2# python3 icmp.py Pinging 31.13.66.112 using Python: Traceback (most recent call last): File "icmp.py", line 149, in <module> ping("www

我正在开发一个ICMP pinger程序,遇到了一个问题。当我运行程序时,我首先在终端中执行sudo su,因此我有根访问权限,因为我们需要使用原始套接字,然后当我运行程序时,我得到这个回溯

sh-3.2# python3 icmp.py
Pinging 31.13.66.112 using Python:

Traceback (most recent call last):
  File "icmp.py", line 149, in <module>
    ping("www.facebook.com")
  File "icmp.py", line 141, in ping
    delay = doOnePing(dest,timeout)
  File "icmp.py", line 123, in doOnePing
    sendOnePing(mySocket,destAddr,myID)
  File "icmp.py", line 111, in sendOnePing
    mySocket.sendto(packet,(destAddr,1))
OSError: [Errno 56] Socket is already connected
这是我正在运行的代码

import os
from socket import *
from sys import *
from struct import *
from time import *
from select import *
from binascii import *

ICMP_ECHO_REQUEST = 8

def checksum (val):

    csum = 0
    countTo = (len(val) // 2) * 2

    count = 0

    while count < countTo:

        thisVal = val[count+1] * 256 + val[count]

        csum = csum + thisVal

        csum = csum & 0xffffffff

        count = count + 2

    if countTo < len(val):

        csum = csum + val[len(val) - 1]

        csum = csum & 0xffffffff

    csum = (csum >> 16) + (csum & 0xffff)

    csum = csum + (csum >> 16)

    answer = ~csum

    answer = answer & 0xffff

    answer = answer >> 8 | (answer << 8 & 0xff00)

    return answer

def receiveOnePing(mySocket,ID,timeout,destAddr):

    timeLeft = timeout


    while 1:

        startedSelect = time()

        whatReady = select([mySocket],[],[],timeLeft)

        howLongInSelect = (time() - startedSelect)

        if whatReady[0] == []: #Timeout

            return "Request timed out."

        timeReceived = time()

        recPacket, addr = mySocket.recvfrom(1024)

        icmpHeader = recPacket[20:28]

        kind,code,checksum,idNum,sequence = unpack("bbHHh",icmpHeader)

        if idNum == ID:

            sizeofdouble = calcsize("d")

            timeSent = unpack("d",recPacket[28:28+sizeofdouble])[0]

            print(("TYPE: %d CODE: %d CHECKSUM: 0x%08x ID: %d SEQ: %d TIME: %d ms\n" % (kind,code,checksum,idNum,sequence,timeReceived - timeSent)*1000))

        timeLeft = timeLeft - howLongInSelect

        if timeLeft <= 0:

            return "Request timed out."

        else:

            return "Reply from %s successfully." % destAddr

def sendOnePing(mySocket, destAddr, ID):

    myChecksum = 0

    header = pack("bbHHh", ICMP_ECHO_REQUEST, 0, myChecksum, ID, 1)

    data = pack("d",time())

    myChecksum = checksum(header + data)

    if platform == 'darwin':

        myChecksum = htons(myChecksum) & 0xffff

    else:

        myChecksum = htons(myChecksum)

    header = 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_RAW,icmp)

    mySocket.connect((destAddr,80))

    myID = os.getpid() & 0xFFFF

    sendOnePing(mySocket,destAddr,myID)

    delay = receiveOnePing(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)

        sleep(1)

    return delay

ping("www.facebook.com")
我们得到了一份包含骨架程序的实验表,我们必须填写缺失的部分,以便

注意:骨架和我的程序不会完全匹配,因为我必须更改一些东西来修复在此之前发生的其他错误

我提前感谢你的帮助

Tyler

因此,连接套接字,即告诉它要向其发送数据的对等地址,然后再次调用sendto提供对等地址。即使这两个地址相同,套接字也会混淆


要么不连接,要么调用send。

您应该使用bind,而不是connect。然后这个程序应该像sudo python3 icmp.py一样在Linux或Mac OS上以root用户身份运行。 希望这有帮助。
谢谢

在创建套接字时,请考虑使用SO_REUSEADDR选项。您如何实现这一点?一般来说,我对套接字编程非常陌生。非常感谢。