Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.send() is raising AttributeError: 'tuple' object has no attribute 'send'

I was developing a program that individual elements from the list to another machine (sender-server,reciever-client). I am sharing my program below

-------------------------client.py------------------------

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 4444                # Reserve a port for your service.

s.connect((host, port))
s.send("Hi server1")

while True:

a=int(s.recv(1024))
tmp=0
while tmp<=a:
    print s.recv(1024)
    tmp=tmp+1

----------------------------------server.py------------------------------

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 4444                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port
s.listen(5)
print "Server listening"
while True:
    c=s.accept()
    b=['a','b']
    d=len(b)
    a=str(d)
    c.send(a)
    for i in range(0,len(b)):
        tmp=str(b[i])
        c.send(tmp)

When I run both server and client, the server raises this:

Traceback (most recent call last):
  File "server.py", line 14, in <module>
    c.send(a)
AttributeError: 'tuple' object has no attribute 'send'
like image 239
Madhvendra Singh Avatar asked Jul 30 '26 13:07

Madhvendra Singh


1 Answers

  1. You'll have to fix the indentation on line 11 of client.py.
  2. socket.accept() returns a tuple (conn, addr) where conn is a socket object. You have to use that object in line 14 to send things. What you're doing is calling send() from the entire tuple which has no method named send and so the AttributeError gets raised. I'd suggest changing line 11 to c = s.accept()[0].
like image 115
Priyansh Agrawal Avatar answered Aug 04 '26 06:08

Priyansh Agrawal