没有退出python套接字程序

时间:2017-10-18 12:15:58

标签: python sockets

我有以下python UDP套接字客户端。我想在按crtl + c时退出程序,但由于该线程没有发生,我该如何解决?

import socket
from threading import Thread
import signal
import sys
message = "well"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('127.0.0.1', 3447)
def sighandler(signum, frame):
    # Close the server
    global sock
    print 'Shutting down server...'
    # Close existing client socket   
    sock.close()
signal.signal(signal.SIGINT, sighandler)

def recv():
    while True:
        print "Waiting to receive data"
        data, server = sock.recvfrom(4096)
        print "Server : ", server
        print "data   : ", data
Thread(target=recv).start()

try:
    while True:
        message = raw_input()
        print >>sys.stderr, 'sending "%s"' % message
        sent = sock.sendto(message, server_address)
except:
    import traceback; traceback.print_exc();

finally:
    sock.close()

1 个答案:

答案 0 :(得分:0)

有时您在线程中只需要:

navigationController?.view.semanticContentAttribute = .forceRightToLeft
navigationController?.navigationBar.semanticContentAttribute =  .forceRightToLeft