python中的多个客户端和一个服务器

时间:2014-12-02 10:31:53

标签: python sockets tcp client-server server

我试图在python中使用TCP创建多个客户端和一个服务器。在sendall上的客户端代码中显示错误:

sock.sendall(bytes(data + "\n", "utf-8"))
TypeError: str() takes at most 1 argument (2 given)

代码:

import socket
import sys

HOST, PORT = "localhost", 9999
data = " ".join(sys.argv[1:])

# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    # Connect to server and send data
    sock.connect((HOST, PORT))
    sock.sendall(bytes(data + "\n", "utf-8"))
    # Receive data from the server and shut down
received = str(sock.recv(1024), "utf-8")
finally:
    sock.close()

print("Sent:     {}".format(data))
print("Received: {}".format(received))

0 个答案:

没有答案