Python 3.x 如何修复不可写套接字对象的类型错误
我正在尝试用python建立聊天室,现在我正在尝试设置用户名和密码,但是当我试图通过dictionary for loop发送数据时,我得到了套接字对象的类型错误 类服务器:Python 3.x 如何修复不可写套接字对象的类型错误,python-3.x,Python 3.x,我正在尝试用python建立聊天室,现在我正在尝试设置用户名和密码,但是当我试图通过dictionary for loop发送数据时,我得到了套接字对象的类型错误 类服务器: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # just a list of connections connection = { } def n_remover(user): with open("UserPass.txt" , 'r') a
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# just a list of connections
connection = {
}
def n_remover(user):
with open("UserPass.txt" , 'r') as f:
passuser = f.readlines()
for x in passuser:
print(x)
for area, value in enumerate(x):
print(value, area)
if value == '\n':
print(value,area)
passuser[passuser.index(x)] = passuser[passuser.index(x)][0:area]
print(passuser[0])
return passuser[passuser.index(user)]
def file_len(file):
with open('UserPass.txt', 'r') as f:
for length, value in enumerate(f):
pass
return length+1
# this bind the server to any ip in ipv4 and any port above 10000
def __init__(self):
self.sock.bind(('0.0.0.0', 10000))
self.sock.listen(1)
# this handles sending out data to all of the users and handels the disconnect of a user the a arg is a ip and port. and c is the threaded connection.
def handler(self, c, a):
while True:
data = c.recv(1024)
print(str(data)[0:1])
if str(data)[0:1] == "/p":
datasplit = (str(self.data)).split(" ")
password_name = [self.datasplit[1] + ' ' + self.datasplit[2]]
with open("UserPass.txt" , 'r+') as f:
passuser = n_remover(password_name)
for key, value in connection:
if c == key:
self.connection[c] = self.datasplit[1]
else:
print(self.connection)
try:
l = self.connection[c]
except IndexError:
self.connection[c] = "anon"
for key,value in self.connection:
key.send(bytes(self.connection[c] + ': ' + data))
if not data:
print(str(a[0]) + ':' + str(a[1], "disconnected"))
del self.connection[c]
c.close
break
我期望它做的只是迭代听写并将数据发送到存储的套接字键,但它会给出
回溯(最近一次呼叫最后一次):
文件“C:\Users\undea\Desktop\Chatroom\Chatroom.py”,第72行,在处理程序中
对于键,self.connection中的值:
TypeError:无法解压缩不可编辑的套接字对象
for key,value in self.connection:
key.send(bytes(self.connection[c] + ': ' + data))
此时self.connection不是一个列表吗?
在这一点上,什么是自我连接?口述?如果是,里面有什么
看起来python无法将内容解包为key,value,因为内容不能拆分为两个值
您似乎正在尝试将套接字对象拆分为键和值。请发布正确的MCVE