我正在写一个简单的聊天,目前只是连接用户。
bootchat/
--server.py
--client.py
--libbootchat/
--bootchat_connection.py
--bootchat_server.py
--bootchat_user.py
代码:
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))
问题:
我启动服务器,然后启动第一个客户端,我发送一个昵称,我没有问题,我的服务器正确打印"新用户连接......",但之后,我开始了一秒钟客户端,我有一个错误。这是我的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
有什么想法吗?谢谢
答案 0 :(得分:2)
为每个连接生成一个新线程:
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()