Python Forum
AttributeError: 'Message' object has no attribute 'split'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'Message' object has no attribute 'split'
#1
I just don't know how to describe anything, because I'm too confused, But all I know is it's supposed to make it into a string but it didn't work. Here is the server code:
god bless buran for this code <3
import json
import socket
import os
from _thread import *
from pathlib import Path

from cryptotools import RSA

UUID = None
lastClient = False
key, pkey = RSA.generate_keypair(1024)
ServerSideSocket = socket.socket()
host = '192.168.1.14'
port = 2004
ThreadCount = 0
response = "NotExists"
try:
    ServerSideSocket.bind((host, port))
except socket.error as e:
    print(str(e))

print('Socket is listening..')
ServerSideSocket.listen(5)


def multi_threaded_client(connection):
    global UUID, lastClient, ThreadCount, key, pkey, response
    while True:
        try:
            data = connection.recv(1024).decode()
            if data != "":
                print(f'Decoded raw data: {data}')
            if data[0:4] == "UUID":  # PKEY
                key, pkey = RSA.generate_keypair(1024)
                Content = data.split("UUID")[1]
                UUID = json.loads(Content)
                msg = json.dumps(pkey)
                connection.sendall(msg.encode())
            else:
                if data == "quit":
                    ThreadCount = ThreadCount - 1
                    break
                data = RSA.Message.from_hex(data)
                data.decrypt(key)
                data = data.str()
                if data[0:4] == "USER":  # USER
                    User = data.split("USER")[1]
                    Users = Path("C:/Users/####/PycharmProjects/pythonProject2/logins.txt")
                    Users = Users.open('r').read().splitlines()
                    for line in Users:
                        if not any(a in line for a in User):
                            response = line
                    response = RSA.Message.from_str(response)
                    response.encrypt(UUID)
                    connection.sendall(response.hex().encode())
                if data[0:4] == "PSWD":  # PSWD
                    Password = data.split("PSWD")[1]
                    PSWD = response.split(":")[1]
                    response = 'Correct' if PSWD == Password else 'Incorrect'
                    message = RSA.Message.from_str(response)
                    message.encrypt(UUID)
                    connection.sendall(message.hex().encode())
        except KeyboardInterrupt:
            print("Exiting")


while True:
    if lastClient:
        break
    try:
        Client, address = ServerSideSocket.accept()
    except KeyboardInterrupt:
        exit(0)
    print('Connected to: ' + address[0] + ':' + str(address[1]))
    start_new_thread(multi_threaded_client, (Client,))
    ThreadCount += 1
    print('Thread Number: ' + str(ThreadCount))
ServerSideSocket.close()
I know I know, the error speaks for itself, but here is the thing, I already converted the Message object to str
Client (just in case):
import atexit
import socket
import json
from time import sleep
import _thread
from cryptotools import RSA
import sys
sys.stdout.write("\rSending public key")
msg = f'UUID{json.dumps(public)}'
s.sendall(msg.encode())
sys.stdout.write("\rWaiting for server public key to be recieved.")
serverkey = json.loads(s.recv(1024).decode())
while True:
    sys.stdout.write("\rPlease Enter your username:                  \n")
    user = "USER" + input()
    user = RSA.Message.from_str(user)
    user.encrypt(serverkey)
    s.sendall(user.hex().replace(' ', '').encode())
    Recieved = s.recv(1024).decode()
    Recieved = RSA.Message.from_hex(Recieved)
    Recieved.decrypt(private)
    Recieved = Recieved.str()
    print(Recieved)
    if Recieved != "NotExists":
        R = Recieved.split(":")[2].split(",")
        contacts.extend(R)
        sys.stdout.seek(0)
        sys.stdout.write("\rPlease Enter A Password:                     \n")
        new_pass = input()
        new_pass = f'PSWD{new_pass}'
        msg = RSA.Message.from_str(new_pass)
        msg.encrypt(serverkey)
        s.sendall(msg.hex().replace(' ', '').encode())
        rawdata = s.recv(1024).decode()
        recieved = RSA.Message.from_hex(rawdata)
        recieved.decrypt(private)
        if str(recieved).split("b'")[1].split("'")[0].find("Correct") != -1:
            loggedin = True
        else:
            loggedin = False
        if loggedin:
            sys.stdout.write("Correct login.")
            for item in contacts:
                if item == "" and counter == 0:
                    sys.stdout.write("no blins :(")
                sys.stdout.write(item + "\n")
                counter = counter + 1
    else:
        sys.stdout.write("Wrong Username! Try again")
        continue
Error:
Traceback (most recent call last): File "C:\Users\####\PycharmProjects\pythonProject2\server.py", line 58, in multi_threaded_client Password = data.split("PSWD")[1] AttributeError: 'Message' object has no attribute 'split'
Reply
#2
Hm. That is strange. I would expect that you should get error on this line

PSWD = response.split(":")[1]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
idk how to delete messages.
Reply
#4
Attribute errors in Python are generally raised when you try to access or call an attribute that a particular object type doesn't possess. The string.split function exists in Python 2.x but not in Python 3.x, where it is only available as an instance method of the str class.

It's simply because there is no attribute with the name you called, for that Object. This means that you got the error when the "module" does not contain the method you are calling. But it is evident that the method is there, which leads to believe that may be the method was added by you in the source code after you had already imported the file (module). Or, some times packages get deprecated and they rename some functions. If that is true, then you may want to exit and reimport the module once again to be able to access the new method .
You can do it in another way to reimport the module with changes without having to exit the interpreter is to do the following:

Quote:reload(myModule)

If you are using python 3.2 or 3.3 you should:

Quote:import imp
imp.reload(myModule)

If running Python 3.4 and up, do import importlib, then do:

Quote:import importlib
importlib.reload(myModule)

The importlib.reload() method reload a previously imported module. The argument must be a module object, so it must
Gribouillis write Dec-14-2021, 07:36 AM:
Please don't necropost with sole purpose to advertise your site.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 148 Jan-19-2022, 08:33 AM
Last Post: menator01
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 218 Jan-02-2022, 09:53 AM
Last Post: snippsat
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 179 Jan-01-2022, 04:16 PM
Last Post: MaartenRo
  How to access parent object attribute Pavel_47 2 400 Nov-19-2021, 09:36 PM
Last Post: deanhystad
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 1,108 Aug-17-2021, 05:24 PM
Last Post: Franky77
  Message='int' object is not subscriptable DanielCook 4 738 Aug-10-2021, 05:21 PM
Last Post: deanhystad
  AttributeError: 'DataFrame' object has no attribute 'Articles' Anldra12 5 2,694 Jun-17-2021, 04:54 PM
Last Post: Larz60+
  AttributeError: 'Node' object has no attribute 'insert' Anldra12 4 2,165 May-11-2021, 10:12 AM
Last Post: Anldra12
  AttributeError: module 'string' has no attribute 'uppercase' Anldra12 10 3,509 Apr-23-2021, 05:30 PM
Last Post: ibreeden
  AttributeError: 'tuple' object has no attribute 'format' Anldra12 7 9,261 Apr-13-2021, 07:45 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020