Python 我在“client_response”变量中收到此错误“TypeError:str() 最多需要 1 个参数(给定 2 个)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42346984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:35:50  来源:igfitidea点击:

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

pythonpython-2.7python-3.xsocketswebsocket

提问by Ahsan Javaid

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

我在“client_response”变量中收到此错误“TypeError:str() 最多需要 1 个参数(给定 2 个)”

回答by francisco sollima

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

回答by Matti Lyra

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

在倒数第二行,您将两个参数传递给str函数,尽管该str函数在 Python 2 中只接受一个参数。实际上在 Python 3 中它确实需要三个参数

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

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.

因此,您要么试图在 python 2 解释器中无意地运行 python 3 代码,要么正在查看错误的语言文档。

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

所以要么使用@franciscosolimas's answer,如果你使用的是python 2,或者确保你使用的是python 3,如果后者你可能还想添加一个关键字参数只是为了确保你知道将来会发生什么

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

回答by JayRizzo

3 arguments, 5 given

3 个参数,5 个给定

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 python3was converting my ,'s to +

为我修复的python3是将我的,'s转换为+

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 passedquery would work. As the commas were doing that in python...

我不得不添加我自己的空间,这样passed查询才能工作。由于逗号在python中这样做......

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

想法和我有根据的猜测:看起来在我的情况下它试图在 bash/python 中进行评估 u'

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

据我所知,这个中断可能是在 bash 中,因为没有调用命令u和/或在 pythonu'中您是否尝试unicode使用不完整的字符串。无论哪种方式,它都坏了,想分享我的修复方法。

Cheers!

干杯!

~JayRizzo

〜杰里佐