While coding a client-server system in python, I came across a weird error in the server stdout, which shouldn't happen:
Traceback (most recent call last):
File "C:\Users\Adam\Drive\DJdaemon\Server\main.py", line 33, in <module>
ClientThread(csock, addr).start()
AttributeError: 'ClientThread' object has no attribute '_initialized'
I split the line up into multiple lines, and it was start() that caused the error.
Any ideas? Here's the server source code- the client just opens and closes the connection:
import socket, threading
class ClientThread(threading.Thread):
def __init__(self, sock, addr):
self.sock = sock
self.addr = addr
def run(self):
sock = self.sock
addr = self.addr
while True:
msg = sock.recv(1024).decode()
if not msg:
print('Disconnect: ' + addr[0] + ':' + str(addr[1]))
sock.close()
return
# Constants
SERVER_ADDRESS = ('', 25566)
MAX_CLIENTS = 10
MCSRV_ADDRESS = ('localhost', 25567)
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.bind(SERVER_ADDRESS)
srv.listen(MAX_CLIENTS)
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
(csock, addr) = srv.accept()
print('Connect: ' + addr[0] + ':' + str(addr[1]))
ClientThread(csock, addr).start()
You forgot to call ClientThread
's parent contructor in __init__
.
def __init__(self, sock, addr):
super(ClientThread, self).__init__()
self.sock = sock
self.addr = addr