我需要一个替代方法来在线程函数中杀死 python 脚本。我的意图是在客户端输入 0 时终止服务器......这是否因为线程尚未终止而不起作用?这是我的代码:
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
print 'Socket created'
try:
socket.bind((HOST, PORT))
except socket.error, message:
print 'Bind statement failed. ERROR: ' + str(message[0]) + ' Message: ' + message[1]
sys.exit()
print 'Socket Binding Successful'
socket.listen(10)
print 'Socket is currently listening'
def clientThread(connection):
while 1:
data = connect.recv(1024)
try:
quit = int(data)
except:
quit = 3
if quit == 0:
print 'Closing the connection and socket...'
connect.close()
socket.close()
sys.exit(); //Alternative needed here...
break
reply = 'Ok....' + data
if not data:
break
connect.sendall(reply)
while 1: #forever loop
connect, address = socket.accept()
print 'Just connected with ' + address[0] + ' : ' + str(address[1])
start_new_thread(clientThread, (connect,))
socket.close()