Python 为什么;类型错误:';非类型';对象没有属性'__获取项目'&引用;如果我的函数中有返回值?
我不明白为什么我的代码返回那个错误, 我已经在登录和目的返回,但它不接受。。。为什么? 我试着把清单放在一个论点中,或者只是 返回一个字符串参数,但它不会更改任何内容Python 为什么;类型错误:';非类型';对象没有属性'__获取项目'&引用;如果我的函数中有返回值?,python,multithreading,Python,Multithreading,我不明白为什么我的代码返回那个错误, 我已经在登录和目的返回,但它不接受。。。为什么? 我试着把清单放在一个论点中,或者只是 返回一个字符串参数,但它不会更改任何内容 Traceback (most recent call last): File "D:/HIGHTS/Documents/Projects/ChatProject/example.py", line 149, in <module> main() File "D:/HIGHTS/Documents/Pr
Traceback (most recent call last):
File "D:/HIGHTS/Documents/Projects/ChatProject/example.py", line 149, in <module>
main()
File "D:/HIGHTS/Documents/Projects/ChatProject/example.py", line 139, in main
account_name = lis[0]
TypeError: 'NoneType' object has no attribute '__getitem__'
请发布完整的追踪。错误发生在哪里?代码关闭,并在“lis=t.join()”之后显示此错误,因此出现错误消息意味着lis为None,因此
t.join()
返回None。在docs()中查找join并查看其状态“As join()始终返回None”,有关获取返回值的方法,请参阅。
class Server():
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.clients = 0
self.clients_dict = {}
self.sock.bind((self.host, self.port))
self.sock.listen(100)
print 'listening...'
def close(self): # the function that responsible of closing the connection between the server and the clients
self.sock.close()
class ClientHandler():
def __init__(self, connection1, connection2):
super(ClientHandler, self).__init__()
self.connection1 = connection1
self.connection2 = connection2
def con1(self):
while True:
data = self.connection1.recv(1024)
self.connection2.send(data)
def con2(self):
while True:
data = self.connection2.recv(1024)
self.connection1.send(data)
def run(self):
t = threading.Thread(target=self.con1)
tt = threading.Thread(target=self.con2)
while True:
t.start()
tt.start()
t.join()
tt.join()
def account_check(name, password, dictionary):
if dictionary[name][0] == password:
return True
else:
return False
def log_in(s, connection, address):
connection.send("Name: ")
account_name = connection.recv(1024)
connection.send("Password: ")
account_password = connection.recv(1024)
if account_name in s.clients_dict:
while not account_check(account_name, account_password, s.clients_dict):
connection.send('False')
connection.send("Name: ")
account_name = connection.recv(1024)
connection.send("Password: ")
account_password = connection.recv(1024)
connection.send('True')
s.clients_dict[account_name][1] = address
s.clients_dict[account_name][3] = 'connected'
else:
connection.send('True')
s.clients_dict[account_name] = [account_password, address, connection, 'connected']
s.clients += 1
a = [account_name, s.clients_dict, s.clients]
print 'dsf ad'
return a
def purpose(connection, dictionary, account_name):
chatter_name = connection.recv(1024)
if chatter_name in dictionary:
if dictionary[chatter_name][3] == 'connected':
client_handler = ClientHandler(dictionary[chatter_name][2], dictionary[account_name][2],)
client_handler.start()
else:
logging.debug("not connected")
else:
logging.debug("there is no such user")
return dictionary
def main():
s = Server('0.0.0.0', 555)
while True and s.clients < 101:
connection, address = s.sock.accept()
t = threading.Thread(target=log_in, args=(s,connection, address,))
t.start()
lis = t.join()
account_name = lis[0]
s.clients_dict = lis[1]
s.clients = lis[2]
tt = threading.Thread(target=purpose, args=(connection, s.clients_dict, account_name,))
tt.start()
s.clients_dict = tt.join()
class Client(object):
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.sock = socket.socket()
def connection(self): # a function that responsible to connect to the server
self.sock.connect((self.ip, self.port))
print self.sock.recv(1024)
self.sock.send(raw_input())
print self.sock.recv(1024)
self.sock.send(raw_input())
ok = self.sock.recv(1024)
if ok == 'True':
ok = True
else:
ok = False
while not ok:
print self.sock.recv(1024)
self.sock.send(raw_input())
print self.sock.recv(1024)
self.sock.send(raw_input())
ok = self.sock.recv(1024)
if ok == 'True':
ok = True
else:
ok = False
print "connected successfully"
x = raw_input("do you want to open a private chat? Y for yes or N for no")
if x == 'Y':
self.private(raw_input("with who?"))
def private(self, client):
self.sock.send(client)
def send_message(self): # a function that responsible to send a message to the server
while True:
self.sock.send(raw_input())
def recieving_message(self): # a function that responsible to receive a message from the server
while True:
data = self.sock.recv(1024)
print data
def main():
c = Client('127.0.0.1', 555)
c.connection() # connect to the server
t1 = threading.Thread(target=c.send_message) # a thread of sending a message
t1.start()
t2 = threading.Thread(target=c.recieving_message) # a thread of recieving a message
t2.start()