idan
idan

Reputation: 5

AttributeError: Class instance has no attribute

I don't know what is the error here

I know that it is very messy but I get an exception after the client connects and send a message

The error id here


Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\Gadi Tepper\PycharmProjects\Connection1\functions.py", line 12, in check_available_message
    user_uid = user.user_uid
AttributeError: Message instance has no attribute 'user_uid'

this is the functions

import time

from message import *
from user import *


def check_available_message(messages, users):
    while True:

        for message in messages:
            for user in users:
                user_uid = user.user_uid
                dest_uid = message.receiver_uid
                if user_uid == dest_uid:
                    print("got here")
                    send_message(messages, message, user.user_socket)


def send_message(messages, message, user_socket):
    print(message.sender_uid + " can send a message to: " + message.sender_uid)
    time.sleep(1)
    data = str((message.receiver_uid + "//" + message.sender_uid + "//" + message.data)).encode
    user_socket.send(data)
    messages.remove(message)

these are the objects

class User:
    def __init__(self, user_uid, user_socket):
        self.user_uid = user_uid
        self.user_socket = user_socket

class Message:
    def __init__(self, sender_uid, receiver_uid, data):
        self.sender_uid = sender_uid
        self.receiver_uid = receiver_uid
        self.data = data

here is my server

from functions import*
from message import*
from user import *
import threading
import socket
import sys

HOST = '0.0.0.0'
PORT = 7979

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #created server

server_socket.bind((HOST, PORT))
    #started bind

server_socket.listen(10)
    #server listening to 10 clients

users = []
messages = []

check_message_thread = threading.Thread(target=check_available_message, args=(users, messages))
check_message_thread.start()

while True:

    (client_socket, address) = server_socket.accept()
        #user connected

    connection_info = client_socket.recv(1024).decode()
        #received user uid and socket

    user, dest_uid = create_user(connection_info, client_socket)
        #created user

    users.append(user)
        #added to users list

    client_socket.send("You connected to server".encode())
        #sent to user connected message

    print("New User Connected: " + user.user_uid)
        #prints new users uid

    receive_message_thread = threading.Thread(target=receive_thread, args=(user.user_uid, user.user_socket, messages, dest_uid))
    receive_message_thread.start()
        #starts a thread that receives new messages

server_socket.close()
    #closes server

and here is the client


from functions import*
from message import*
from user import *
import threading
import socket
import sys


HOST = '127.0.0.1'  # The server's hostname or IP address
PORT = 7979        # The port used by the server

client_socket = socket.socket()
client_socket.connect((HOST, PORT))

user_info = str("uid1")
dest_info = str("uid2")

data_send = str(user_info + "//" + dest_info + "//")
client_socket.send(data_send.encode())


while True:

    receive_message_thread = threading.Thread(target=client_receive_thread, args=(user_info, client_socket))
    receive_message_thread.start()

    print (client_socket.recv(1024).decode())
    i = 5
    while i > 0:
        data = raw_input("enter message :")
        client_socket.send(str(data_send + data).encode())
        i + 1

client_socket.close()


I am really sorry that this is very messy but I wasn't sure what eas the problem here

Upvotes: 0

Views: 273

Answers (2)

Rayan Ral
Rayan Ral

Reputation: 1859

You have a wrong parameter order in your server. In functions.py, you're accepting check_available_message(messages, users), and in server you have another order - args=(users, messages)

Upvotes: 0

Chris
Chris

Reputation: 23171

You're passing your arguments backwards. You set up the thread like this:

target=check_available_message, args=(users, messages))

but your method expects messages as the first param and users as the second:

def check_available_message(messages, users):

Upvotes: 1

Related Questions