Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am getting this error "TypeError: str() takes at most 1 argument (2 given)" at "client_response" variable

EDIT to format:

This is the original code

from __future__ import print_function
import socket
import sys

def socket_accept():
    conn, address = s.accept()
    print("Connection has been established | " + "IP " + address[0] + "| Port " + str(address[1]))
    send_commands(conn)
    conn.close()

def send_commands(conn):
    while True:
        cmd = raw_input()
        if cmd == 'quit':
            conn.close()
            s.close()
            sys.exit()
        if len(str.encode(cmd)) > 0:
            conn.send(str.encode(cmd))
            client_response = str(conn.recv(1024), "utf-8")
            print(client_response, end ="")

def main():
    socket_accept()
    main()

I am getting this error “TypeError: str() takes at most 1 argument (2 given)” at “client_response” variable

like image 605
Ahsan Javaid Avatar asked Feb 20 '17 14:02

Ahsan Javaid


2 Answers

You have your error here:

client_response = str(conn.recv(1024), "utf-8")

Just change it to:

client_response = str(conn.recv(1024)).encode("utf-8")
like image 185
francisco sollima Avatar answered Nov 17 '22 16:11

francisco sollima


On the second to last line you're passing two arguments to the str function, although the str function only takes a single argument in Python 2. It does in fact take up to three arguments in python 3

https://docs.python.org/2.7/library/functions.html?highlight=str#str https://docs.python.org/3.6/library/functions.html?highlight=str#str

So you're either trying to inadvertaetly run python 3 code in a python 2 interpreter or you're looking at the wrong language documentation.

So either use @franciscosolimas's answer, if you're using python 2, or make sure you're using python 3, if the latter you might also want to add a keyword argument just to make sure you know what's happening in the future

client_response = str(conn.recv(1024), encoding="utf-8")
like image 31
Matti Lyra Avatar answered Nov 17 '22 17:11

Matti Lyra