TypeError: send() argument after * must be an iterable, not socket in Python

Dung Do Tien Sep 23 2021 185

Hello you guys, I am a newbie in Python and I'm also studying more about Python.

I have some lines of code, I have try to work with multi threading in Python and help me send a message with socket. Function as below:

import threading, socket

uri = ("localhost", 8080)

def send(sock):
    sock.sendto("Message", uri)
    print("Message sent")

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
threading.Thread(target=send, args=(s)).start()

But I get an exception TypeError: send() argument after * must be an iterable, not socket when I run code above.

Exception in thread Thread-1:
 Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: send() argument after * must be an iterable, not socket

And I am using python 3.8.2

Anyone can explain it to me? How can I solve it?

Thanks for any response.

Have 1 answer(s) found.
  • E

    Eslam Ali Sep 23 2021

    You need to add a comma , after the variable s. Sending only s to args= () is an attempt to unpack multiple arguments instead of sending just one argument.

    Change:

    threading.Thread(target=send, args=(s)).start()

    To

    threading.Thread(target=send, args=(s,)).start()

    I hope it useful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close