On Thursday, February 25, 2016 at 12:56:10 PM UTC-5, Ray wrote: > hi, > > I'm new to python networking. I am waiting TCP server/client app by using > python built-in SocketServer. My problem is if client get killed, then the > tcp port will never get released, in CLOSE_WAIT > > maybe I didn't do the handler right? or anyway I can catch the client get > killed? > > I wrote following simple code to explain the issue I have, server listen on > 127.0.0.1:1234 > client need to send multi-messages (I am sending 2 in this test) if client > get killed (I just do kill client_pid) then if I check lsof, I will see the > new allocated tcp do not get released > > ########server.py######## > > #!/usr/bin/python > import threading > import SocketServer > import time > > class TCPServerRequestHandler(SocketServer.BaseRequestHandler): > > def handle(self): > while True: > data=self.request.recv(1024) > if not data: > time.sleep(0.1) > continue > print data > self.request.send(data) > if data=='end': > break > > def main(): > server = TCPServer(('127.0.0.1', 1234), TCPServerRequestHandler) > ip, port = server.server_address > server_thread = threading.Thread(target=server.serve_forever) > server_thread.daemon = True > server_thread.start() > time.sleep(600) > server.shutdown() > server.server_close() > if __name__=='__main__': > main() > > ########client.py######## > import socket > import time > > def client(): > sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) > sock.connect(('127.0.0.1', 1234)) > sock.sendall('hello') > response=sock.recv(1024) > print "Received:", response > time.sleep(60) > sock.sendall('end') > response=sock.recv(1024) > print "Received:", response > sock.close() > > if __name__=='__main__': > client() > > > when I run it, client has 60 seconds sleep, so I just kill the process from OS > kill 93948 > > then I'm running lsof to find my TCP/UDP > lsof -n -p $(ps -ef|grep serve[r]|awk '{print $2}')|egrep '(TCP|UDP)'|awk > '{print $NF}' > > TCP 127.0.0.1:search-agent (LISTEN) > TCP 127.0.0.1:search-agent->127.0.0.1:59411 (CLOSE_WAIT) > > I will see the CLOSE_WAIT, this is where the client get killed. > > > any help would be great! > > Thanks a lot!
I fond the issue. it's my own stupid issue. i did "continue" if no data received. just break from it then it will be fine -- https://mail.python.org/mailman/listinfo/python-list