Python : threads can only be started once
<p>I'm writing a simple chat, for the moment it just connect an user.</p>

<pre><code>bootchat/
  --server.py
  --client.py
  --libbootchat/
    --bootchat_connection.py
    --bootchat_server.py
    --bootchat_user.py
</code></pre>

<p>Code:</p>

<p><strong>client.py</strong></p>

<pre><code># imports
import libbootchat.bootchat_user as user
import socket

print("Welcome on BootChat")


nickname = input("nickname : ")

bootchat_user = user.BootchatUser(nickname)
</code></pre>

<p><strong>server.py</strong></p>

<pre><code># 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()
</code></pre>

<p><strong>bootchat_connection.py</strong></p>

<pre><code>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()
</code></pre>

<p><strong>bootchat_user.py</strong></p>

<pre><code>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)
</code></pre>

<p><strong>bootchat_server.py</strong></p>

<pre><code>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))
</code></pre>

<p>Problem:</p>

<p>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 :</p>

<pre><code>    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 &lt;module&gt;
    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
</code></pre>

<p>Any ideas ? Thank you</p>