Alexandre Markiewicz Alexandre Markiewicz - 1 month ago 22
Python Question

Python : threads can only be started once

I'm writing a simple chat, for the moment it just connect an user.

bootchat/
--server.py
--client.py
--libbootchat/
--bootchat_connection.py
--bootchat_server.py
--bootchat_user.py


Code:

client.py

# imports
import libbootchat.bootchat_user as user
import socket

print("Welcome on BootChat")


nickname = input("nickname : ")

bootchat_user = user.BootchatUser(nickname)


server.py

# imports
import socket
import signal
import libbootchat.bootchat_server as server
import libbootchat.bootchat_connection as connection

print("Starting server...")
bootchat_server = server.BootchatServer()



while True:
bootchat_server.accept_connection()
bootchat_server.start()


bootchat_connection.py

class BootchatConnection():

def __init__(self, socket, informations=None):
self.socket = socket

if informations:
self.ip = informations[0]
self.port = informations[1]

def connect_to_server(self, ip, port):
self.socket.connect((ip, port))

def send_to_server(self, msg):
self.socket.send(msg.encode())

def recv_from_client(self):
return self.socket.recv(1024).decode()


bootchat_user.py

from libbootchat.bootchat_connection import BootchatConnection
import socket

class BootchatUser:
nb_users = 0

def __init__(self, nickname):
self.nickname = nickname
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.bootchat_connection = BootchatConnection(connection)
self.bootchat_connection.connect_to_server("localhost", 15297)
self.send_message(nickname)

def send_message(self, msg):
self.bootchat_connection.send_to_server(msg)


bootchat_server.py

import socket
from threading import Thread
from libbootchat.bootchat_connection import BootchatConnection

class BootchatServer(Thread):

def __init__(self):
Thread.__init__(self)
self.listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.listener.bind(('', 15297))
self.listener.listen(5)

def accept_connection(self):
socket, infos = self.listener.accept()
self.bootchat_connection = BootchatConnection(socket, infos)

def run(self):
print("[INFO] New user connected from {}".format(self.bootchat_connection.ip))
nickname = self.bootchat_connection.recv_from_client()
print("[INFO] User from {} now logged at {}".format(self.bootchat_connection.ip, nickname))


Problem:

I start server and i start a first client, and I send a nickname and I have no problems, my server print correctly "New user connected...", but after, I start a second client, and I have an error. This is the output of my server.py :

Starting server...
[INFO] New user connected from 127.0.0.1
[INFO] User from 127.0.0.1 now logged at Alex
Traceback (most recent call last):
File "server.py", line 14, in <module>
bootchat_server.start()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 840, in start
raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once


Any ideas ? Thank you

Answer

Generate a new thread for each connection:

class BootchatServer(object):

    def __init__(self):
        self.listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.listener.bind(('', 15297))
        self.listener.listen(5)

    def accept_connection(self):
        socket, infos = self.listener.accept()
        connection = BootchatConnection(socket, infos)
        thread = Thread(target=self.process_connection, args=(connection,))
        thread.start()

    def process_connection(self, connection):
        print("[INFO] New user connected from {}".format(connection.ip))
        nickname = connection.recv_from_client()
        print("[INFO] User from {} now logged at {}".format(connection.ip, nickname))

print("Starting server...")
bootchat_server = server.BootchatServer()

while True:
    bootchat_server.accept_connection()