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

蹲街弑〆低调 提交于 2021-02-18 22:55:43

问题


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


回答1:


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")



回答2:


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")



回答3:


3 arguments, 5 given

I got a similar error, may not be the same here (as the op) but, it was simple enough fix and wanted to share, since I ended up here from my searches on the error.

Traceback (most recent call last):
File "queries.py", line 50, in <module>
"WHERE ao.type='u';")
TypeError: str() takes at most 3 arguments (5 given)`

What fixed it for me in python3 was converting my ,'s to +

Error:

str("SELECT s.name + '.' + ao.name, s.name"
            "FROM sys.all_objects ao",
            "INNER JOIN sys.schemas s",
            "ON s.schema_id = ao.schema_id",
            "WHERE ao.type='u';"))

Fixed:

str("SELECT s.name + '.' + ao.name, s.name " +
            "FROM sys.all_objects ao " +
            "INNER JOIN sys.schemas s " +
            "ON s.schema_id = ao.schema_id " +
            "WHERE ao.type='u';")

I had to add my own spaces so the passed query would work. As the commas were doing that in python...

Thoughts & my educated guess: looks like in my case it got caught up trying to evaluate in bash/python a litteral u'

To my knowledge this break could be in bash because there is no command called u and/or in python u' is you trying to unicode an incomplete string. Either way it broke and wanted to share my fix.

Cheers!

~JayRizzo



来源:https://stackoverflow.com/questions/42346984/i-am-getting-this-error-typeerror-str-takes-at-most-1-argument-2-given-at

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!