在线程线程中获取错误消息异常时,如何解决Python线程问题:
因此,我正在尝试创建一个服务器和客户端系统,该服务器和客户端系统从客户端接收输入并将其显示在服务器端,但是我遇到了一个问题,我只能每个客户端发送1个输入信号,然后我得到
File "client.py", line 25, in <module>
send(input())
File "client.py", line 20, in send
client.send(message)
BrokenPipeError: [Errno 32] Broken pipe
在服务器端,我在发送输入消息时会收到以下消息。
[STARTING] server is starting ...
[LISTENING] server is listening on 127.0.1.1
[NEW CONNECTION] ('127.0.0.1', 60468) connected.
[active connections] 1
[('127.0.0.1', 60468)] hello
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "server.py", line 21, in handle_client
msg_length = connection.recv(HEADER).decode(FORMAT)
OSError: [Errno 9] Bad file descriptor
服务器文件
import socket
import threading
PORT = 5054
HEADER = 64
SERVER = socket.gethostbyname(socket.gethostname())
ADDRESS = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MSG= "DISCONNECT"
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv.bind(ADDRESS)
def handle_client(connection,addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected = True
while connected:
msg_length = connection.recv(HEADER).decode(FORMAT)
if msg_length :
msg_length = int(msg_length)
msg = connection.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MSG:
connected = False
print(f"[{addr}] {msg}")
connection.close()
def start():
serv.listen()
print(f"[LISTENING] server is listening on {SERVER}")
while True:
connection,addr = serv.accept()
thread = threading.Thread(target=handle_client, args=(connection,addr))
thread.start()
print(f"[active connections] {threading.active_count()-1}")
print("[STARTING] server is starting ...")
start()
和客户端文件为
import socket
PORT = 5054
HEADER = 64
SERVER = socket.gethostbyname(socket.gethostname())
ADDRESS = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MSG= "DISCONNECT"
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDRESS)
def send(msg):
message = msg.encode(FORMAT)
msg_length = len (message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length)
client.send(message)
while True:
send(input())
so I'm trying to create a server and client system that takes in inputs from the clients and displays it on the server-side , but I'm running into an issue that I can only send 1 input signal per client and then I get
File "client.py", line 25, in <module>
send(input())
File "client.py", line 20, in send
client.send(message)
BrokenPipeError: [Errno 32] Broken pipe
and on the server side i get the following message when sending an input message
[STARTING] server is starting ...
[LISTENING] server is listening on 127.0.1.1
[NEW CONNECTION] ('127.0.0.1', 60468) connected.
[active connections] 1
[('127.0.0.1', 60468)] hello
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "server.py", line 21, in handle_client
msg_length = connection.recv(HEADER).decode(FORMAT)
OSError: [Errno 9] Bad file descriptor
the server file
import socket
import threading
PORT = 5054
HEADER = 64
SERVER = socket.gethostbyname(socket.gethostname())
ADDRESS = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MSG= "DISCONNECT"
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv.bind(ADDRESS)
def handle_client(connection,addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected = True
while connected:
msg_length = connection.recv(HEADER).decode(FORMAT)
if msg_length :
msg_length = int(msg_length)
msg = connection.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MSG:
connected = False
print(f"[{addr}] {msg}")
connection.close()
def start():
serv.listen()
print(f"[LISTENING] server is listening on {SERVER}")
while True:
connection,addr = serv.accept()
thread = threading.Thread(target=handle_client, args=(connection,addr))
thread.start()
print(f"[active connections] {threading.active_count()-1}")
print("[STARTING] server is starting ...")
start()
and the client file is
import socket
PORT = 5054
HEADER = 64
SERVER = socket.gethostbyname(socket.gethostname())
ADDRESS = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MSG= "DISCONNECT"
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDRESS)
def send(msg):
message = msg.encode(FORMAT)
msg_length = len (message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length)
client.send(message)
while True:
send(input())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论