gpt4 book ai didi

python - 属性错误: '_Helper' object has no attribute 'encode'

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:02 25 4
gpt4 key购买 nike

当我输入 help 时,我将客户端连接到服务器,我收到 AttributeError: '_Helper' object has no attribute 'encode'

这是完整的输出

-:-:-:-:-:Server:-:-:-:-:-
Connection from: ('127.0.0.1', 58675)
Your Instruction: help
Traceback (most recent call last):
File "serverback.py", line 36, in <module>
msg = msg.encode("UTF-8")
AttributeError: '_Helper' object has no attribute 'encode'

这是我的Python脚本

#imports
import socket
import os
import sys
#Variables
port = int(sys.argv[1])

#Functions

def clear():
os.system('cls' if os.name=='nt' else 'clear')

#Starting Server
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
serversocket.bind((host, port))
serversocket.listen(1)

clear()
print("-:-:-:-:-:Py Server:-:-:-:-:-")
clientsocket, addr = serversocket.accept()
print("Connection from: " + str(addr))
while True:
msg = input("Your Instruction: ")

if msg == "help":
clear()
print("-+-+-+-+-+HELP+-+-+-+-+-")
print("Test Connection: 'test'")

input("\nPress ENTER to continue")
clear()
print("-:-:-:-:-:Py Server:-:-:-:-:-")

else:
msg = msg.encode("UTF-8")
clientsocket.send(msg)
msg = clientsocket.recv(4096)
print(msg.decode("UTF-8"))

最佳答案

msg 是否由于某种原因未定义?在 Python3+ 中,输入应始终返回一个字符串,但由于某种原因,当满足 else 条件时,您的 msg 变量似乎不是字符串。您可以将 msg 显式转换为字符串。

else:
msg = str(msg).encode("UTF-8")
clientsocket.send(msg)
msg = clientsocket.recv(4096)
print(msg.decode("UTF-8"))

关于python - 属性错误: '_Helper' object has no attribute 'encode' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45643402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com