Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103510
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2016-02-25 09:55 -0800 |
| Message-ID | <7ff15c67-102e-4b02-a2c8-87b24f1b8351@googlegroups.com> (permalink) |
| Subject | tcp networking question (CLOSE_WAIT) |
| From | Ray <rui.vapps@gmail.com> |
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!
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
tcp networking question (CLOSE_WAIT) Ray <rui.vapps@gmail.com> - 2016-02-25 09:55 -0800
Re: tcp networking question (CLOSE_WAIT) "Martin A. Brown" <martin@linux-ip.net> - 2016-02-25 10:17 -0800
Re: tcp networking question (CLOSE_WAIT) Ray <rui.vapps@gmail.com> - 2016-02-25 10:25 -0800
Re: tcp networking question (CLOSE_WAIT) "Martin A. Brown" <martin@linux-ip.net> - 2016-02-25 10:56 -0800
Re: tcp networking question (CLOSE_WAIT) Ray <rui.vapps@gmail.com> - 2016-02-25 10:59 -0800
Re: tcp networking question (CLOSE_WAIT) Ray <rui.vapps@gmail.com> - 2016-02-25 10:44 -0800
csiph-web