如何在 python 中中断 socket.recv()?
How to interrupt socket.recv() in python?
我正在开发一个小型服务器系统,每当我在控制台中输入 "exit()" 时都需要关闭服务器(输入由另一个线程处理)
我想知道是否有一种方法可以在套接字等待数据时终止主线程。我已经尝试在 try 块中使用 _thread.interrupt_main() 和 keyboardInterrupt 异常,但它没有用。
我还尝试了 os._exit() ,但它没有清理,所以我决定不使用它。
我的代码:
import socket
import _thread
import os
clear = lambda: os.system("cls")
try:
Server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
Server.bind(("localhost",port))
Server.listen(2)
clear()
print("--------------------------------------")
print("Server running on port %s" % str(port))
print("--------------------------------------")
except Exception:
print("error while starting server")
input()
exit()
def control():
while True:
command = input()
if command == "exit()":
#interrupt code here
_thread.start_new_thread(control,())
while True:
con,ip = Server.accept()
print(str(ip) + " Connected")
try:
cmd = str(con.recv(1024).decode()) #<-- interrupt this line
except Exception:
print("Error")
下面的代码执行您想要的操作,但在基本级别上用于关闭单个客户端的连接。
如果愿意,您应该重组代码以处理多个客户端。
最好的办法是为同一套接字连接的每个连接启动新线程,以便您可以单独处理它们。
import socket
import _thread
import os
clear = lambda: os.system("cls")
port = 1026
try:
Server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
Server.bind(("127.0.0.1",1026))
Server.listen(2)
print("--------------------------------------")
print("Server running on port %s" % str(port))
print("--------------------------------------")
except Exception:
print("error while starting server")
input()
exit()
def control(sock):
while True:
command = input()
if command == "exit()":
sock.close()
os._exit(0)
#interrupt code here
while True:
con,ip = Server.accept()
_thread.start_new_thread(control,(con,))
print(str(ip) + " Connected")
try:
cmd = str(con.recv(1024).decode()) #<-- interrupt this line
except Exception:
print("Error")
sock.shutdown(socket.SHUT_RDWR)
如果您在主线程中有套接字并且线程在 recv/recvfrom/etc 中被阻塞,则非常方便。 recv 调用将以异常结束,如果需要,您可以使用它来完成您的线程。
我正在开发一个小型服务器系统,每当我在控制台中输入 "exit()" 时都需要关闭服务器(输入由另一个线程处理) 我想知道是否有一种方法可以在套接字等待数据时终止主线程。我已经尝试在 try 块中使用 _thread.interrupt_main() 和 keyboardInterrupt 异常,但它没有用。 我还尝试了 os._exit() ,但它没有清理,所以我决定不使用它。 我的代码:
import socket
import _thread
import os
clear = lambda: os.system("cls")
try:
Server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
Server.bind(("localhost",port))
Server.listen(2)
clear()
print("--------------------------------------")
print("Server running on port %s" % str(port))
print("--------------------------------------")
except Exception:
print("error while starting server")
input()
exit()
def control():
while True:
command = input()
if command == "exit()":
#interrupt code here
_thread.start_new_thread(control,())
while True:
con,ip = Server.accept()
print(str(ip) + " Connected")
try:
cmd = str(con.recv(1024).decode()) #<-- interrupt this line
except Exception:
print("Error")
下面的代码执行您想要的操作,但在基本级别上用于关闭单个客户端的连接。 如果愿意,您应该重组代码以处理多个客户端。 最好的办法是为同一套接字连接的每个连接启动新线程,以便您可以单独处理它们。
import socket
import _thread
import os
clear = lambda: os.system("cls")
port = 1026
try:
Server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
Server.bind(("127.0.0.1",1026))
Server.listen(2)
print("--------------------------------------")
print("Server running on port %s" % str(port))
print("--------------------------------------")
except Exception:
print("error while starting server")
input()
exit()
def control(sock):
while True:
command = input()
if command == "exit()":
sock.close()
os._exit(0)
#interrupt code here
while True:
con,ip = Server.accept()
_thread.start_new_thread(control,(con,))
print(str(ip) + " Connected")
try:
cmd = str(con.recv(1024).decode()) #<-- interrupt this line
except Exception:
print("Error")
sock.shutdown(socket.SHUT_RDWR)
如果您在主线程中有套接字并且线程在 recv/recvfrom/etc 中被阻塞,则非常方便。 recv 调用将以异常结束,如果需要,您可以使用它来完成您的线程。