0

I am trying to make multiple clients and one server using TCP in python. In my client code on sendall its showing error:

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

Code:

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))
fredtantini
  • 15,966
  • 8
  • 49
  • 55
shilpi_agrawal
  • 108
  • 1
  • 9
  • Are you trying to run this on Python 2? – PM 2Ring Dec 02 '14 at 11:10
  • In Python 2, `bytes()` is just a synonym for `str()` and so it only takes a single arg. See http://stackoverflow.com/questions/5901706/the-bytes-type-in-python-2-7-and-pep-358 – PM 2Ring Dec 12 '14 at 05:19

0 Answers0