0

i dont understand why my code returns that ERROR, i have return in log_in and purpose but it doesnt accept that... why?

i have tried to put the list in an argument a or just returns a string argument but it doesnt change anything...

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__'

server code:

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

client code:

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()
shahar
  • 79
  • 2
  • 2
  • 5
  • Post the full traceback, please. Where does the error occur? – RemcoGerlich Jan 07 '17 at 20:40
  • the code shut down and show this error right after the " lis = t.join() " – shahar Jan 07 '17 at 20:51
  • 1
    So there error message means that lis is None, so `t.join()` returned None. Lookup join in the docs ( https://docs.python.org/2/library/threading.html#threading.Thread.join ) and see that it states "As join() always returns None," – RemcoGerlich Jan 07 '17 at 20:59
  • See http://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread-in-python for ways to get the return value. – RemcoGerlich Jan 07 '17 at 21:01

0 Answers0