python - How can I stop udp server in threading? -


i wrote udp no echo server in program, used thread running , listen message send others. seems couldn't stop use tl.stop(), when input q or quit. of code following:

            class treadlisten(threading.thread):                 def __init__(self):                     self.running = true                     threading.thread.__init__(self)                  def run(self):                     address = ('localhost', 16666)                     sock = socket.socket(socket.af_inet, socket.sock_dgram)                     sock.bind(address)                     while true:                         data = sock.recv(65535)                         print "message:{0}".format(data)                     sock.close()                  def stop(self):                     self.running = false                 # end of class thread_clock               if __name__ == "__main__":                 tl = treadlisten()                 tl.start()                  while true:                     message = raw_input("cmd>")                     if not message:                         print "please input command!"                         continue                     elif (message == 'quit') or (message == 'q'):                         tl.stop()                         break                     else:                         print "input {0}".format(message)                         #                         continue                 print "[connection closed!]" 

i trying add sock.shutdown(socket.shut_rdwr) , sock.close() def stop of class, doesn't work. how can stop thread safety? thanks!

your while loop while true: works forever, guess close or shutdown calls socket never can gets in way work.

you should change while true: while self.running: , should trick.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -