TypeError: "'str' object cannot be interpreted as integer using" Python's dpkt and socket modules

50 Views Asked by At

Hello sorry of this is a dumb question but I'm trying to send an ICMP echo packet using the socket module's sendto() function. My socket is defined as:

def make_socket(timeToLive, timeout):

    mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    mySocket.settimeout(timeout)
    mySocket.setsockopt(socket.IPPROTO_IP, socket.IP_TTL, timeToLive)
    return mySocket

I then create an ICMP echo packet inside the following function and try to send it, but this is where I'm getting the type error:

def send_echo(socket, pay, id, seq, dest):
    echo = dpkt.icmp.ICMP.Echo()
    echo.id = id
    echo.seq = seq
    echo.payload = bytes(pay, 'utf-8')
    
    icmp = dpkt.icmp.ICMP()
    icmp.type = dpkt.icmp.ICMP_ECHO
    icmp.data = echo
    icmp_packed = icmp.pack()
    socket.sendto(icmp_packed, dest)

The problem is the last line socket.sendto(icmp_packed, dest) producing the error

line 42, in send_echo
    socket.sendto(icmp_packed, destination)
TypeError: 'str' object cannot be interpreted as an integer

I guess I'm a little confused on what's producing this error, because icmp_packed is of type <class 'bytes'> which is correct and dest is defined in my main function as:

def main():
    host_port = tuple(["127.0.0.1", "65432"])
    send_icmp_echo(make_socket(5,5), "data", 5, 5, host_port)

keep in mind these values are just for testing purposes. I'm just trying to get it to compile at the moment. Is the type error caused from the icmp_packed or dest in socket.sendto? I used bytes(pay, 'utf-8' to convert the payload into class bytes but that didn't seem to help at all. Or is this a bug in my definitions? Any clarification/help would be appreciated.

0

There are 0 best solutions below