3

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'
Priyansh Agrawal
  • 330
  • 2
  • 19

1 Answers1

4
  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].
Priyansh Agrawal
  • 330
  • 2
  • 19
  • Hi @Priyansh , your steps seem to work. The output produced in the client are the characters `a` and `b` in two separate lines. Isn't this line `s.send("Hi server1")` supposed to send a message "Hi server1" to the server as well? I'm not sure what the OP wants, but your code works 100%. – hridayns Jun 30 '17 at 05:00
  • Hey @code_byter, that line is definitely sending the message. The problem is that OP is not printing whatever the server receives. Just add this "print c.recv(1024)" after "c=s.accept()[0]" and you'll see that message. – Priyansh Agrawal Jun 30 '17 at 06:33