Erik
Erik

Reputation: 23

I understad why I get: TypeError: getsockaddrarg: AF_INET address must be tuple, not str

Okey, I have been staring at this two codes for a long time, and I can't get why I receive this error: TypeError: getsockaddrarg: AF_INET address must be tuple, not str.

code1.py: import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

import echoUDP

serveraddress = '0.0.0.0'
serverport = 5002

server2 = (server_address, server_port)
s.bind(server2)
print("Listening on " + server_address + ":" + str(server_port))
s.connect(('0.0.0.0',5005))

while True:
    client_address = ('0.0.0.0.')
    status = 'ok'
    print("Echoing back"+ str(status) + " to " + str(client_address))
    sen = s.sendto(status.encode(),clientaddress)

echoUDP.py:

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

server_address = '0.0.0.0'
server_port = 5005

server = (server_address, server_port)
sock.bind(server)
print("Listening on " + server_address + " Port: " + str(server_port))
sock.connect(('0.0.0.0', 5002))
while True:
    client_address = ('0.0.0.0')
    status = 'ok'
    print("Echoing back "+ str(status) + " to " + str(client_address))
    sen = sock.sendto(status.encode(),client_address)

Can someone please tell what I don't get? I have read the other problems about tuple, and they get it because their sendto line, the client_address is not a tuple. I thought I created this as a tuple when I write client_address = ('0.0.0.0')?

Upvotes: 1

Views: 1606

Answers (1)

sjw
sjw

Reputation: 6543

The second argument to sock.sendto() should be a tuple containing the host and the port - e.g. ('0.0.0.0', 5005). So in your code, use:

sen = sock.sendto(status.encode(), (server_address, server_port))

As an aside, ('0.0.0.0') is not a tuple. ('0.0.0.0',) would be a tuple of length 1. Parentheses are optional for tuple creation in Python - it's the presence of a comma that tells Python that you're creating a tuple. These examples should illustrate.

a = 1,
type(a)
>>> tuple

b = 1, 2
type(b)
>>> tuple

c = (1)
type(c)
>>>> int

Upvotes: 1

Related Questions