[Python] How do I abort a socket.recv() from another thread in Python

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    I have a main thread that waits for connection. It spawns client threads that will echo the response from the client (telnet in this case). But say that I want to close down all sockets and all threads after some time, like after 1 connection.

    How would I do it? If I do clientSocket.close() from the main thread, it won't stop doing the recv. It will only stop if I first send something through telnet, then it will fail doing further sends and recvs.

    My code looks like this:

    # Echo server program
    import socket
    from threading import Thread
    import time

    class ClientThread(Thread):
    def __init__(self, clientSocket):
    Thread.__init__(self)
    self.clientSocket = clientSocket

    def run(self):
    while 1:
    try:
    # It will hang here, even if I do close on the socket
    data = self.clientSocket.recv(1024)
    print "Got data: ", data
    self.clientSocket.send(data)
    except:
    break

    self.clientSocket.close()

    HOST = ''
    PORT = 6000
    serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    serverSocket.bind((HOST, PORT))
    serverSocket.listen(1)

    clientSocket, addr = serverSocket.accept()
    print 'Got a new connection from: ', addr
    clientThread = ClientThread(clientSocket)
    clientThread.start()

    time.sleep(1)

    # This won't make the recv in the clientThread to stop immediately,
    # nor will it generate an exception
    clientSocket.close()

    Continue reading...

Compartilhe esta Página