Python 2.7 socket.send()正在引发AttributeError:';元组';对象没有属性';发送';

Python 2.7 socket.send()正在引发AttributeError:';元组';对象没有属性';发送';,python-2.7,python-sockets,Python 2.7,Python Sockets,我正在开发一个程序,将列表中的各个元素发送到另一台机器(发送方服务器、接收方客户端)。我在下面分享我的程序 -------------------------client.py------------------------ import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Ge

我正在开发一个程序,将列表中的各个元素发送到另一台机器(发送方服务器、接收方客户端)。我在下面分享我的程序

-------------------------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
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)
当我同时运行服务器和客户端时,服务器会引发以下问题:

Traceback (most recent call last):
  File "server.py", line 14, in <module>
    c.send(a)
AttributeError: 'tuple' object has no attribute 'send'
回溯(最近一次呼叫最后一次):
文件“server.py”,第14行,在
c、 发送(a)
AttributeError:“元组”对象没有属性“发送”
1)您必须修复client.py第11行的缩进
2) 返回一个元组(conn,addr),其中conn是套接字对象。您必须使用第14行中的对象来发送内容。您所做的是从整个元组调用send(),并且没有名为send的方法。因此,它引发了错误。我建议把11号线改为

c = s.accept()[0]

您的客户端代码在
a=int(s.recv(1024))
Hi@priyanhs处没有正确缩进,您的步骤似乎正常。客户端中生成的输出是两行中的字符
a
b
。这行
s.send(“Hi server1”)
不也应该向服务器发送消息“Hi server1”吗?我不确定OP想要什么,但你的代码100%有效。嘿@code_byter,那行肯定是在发送信息。问题是OP没有打印服务器接收到的任何内容。只需在“c=s.accept()[0]”之后添加这个“print c.recv(1024)”,您就会看到该消息。