Wie schließen socket-Verbindung auf Strg-C in einem python-Programm

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)

any_connection = False

while True:
    try:
        conn, addr = s.accept()
        data = conn.recv(1024)
        any_connection = True

        # keep looking
        if not data: continue

        pid = os.fork()

        if pid == 0:
            server_process(data, conn)
    except KeyboardInterrupt:
        break

if any_connection:
    print("Closing connection")
    conn.close()

Ich bin, fangen die KeyboardInterruptsignal hier auf eine unendliche -, TCP-server, den ich geschrieben in Python. Allerdings, obwohl ich weiß, es ist das schließen der Verbindung, da es druckt Closing Connection, wenn ich versuche, re-run der server habe ich bekommen:

OSError: [Errno 48] Address already in use

Ich habe keine Ahnung, was passiert, weil ich weiß, für sicher, dass ich den Aufruf conn.close().

Und läuft killall python3fix nicht, ich erhalte die Fehlermeldung, wenn ich warten, für eine lange Zeit oder ändern port. Auch habe ich versucht zu grep alle python3 Prozesse, aber ich bekomme nichts.

Ich bin mit OS X Yosemite.

InformationsquelleAutor David Gomes | 2014-12-08
Schreibe einen Kommentar