python socket handle keepalive request -
i using python write simple web server, , sending requests it. , use libevent http client. every time send keep-alive request, http connection have close callback before success callback. think might keep-alive problem. , python(server) code:
import socket host, port = '', 8999 listen_socket = socket.socket(socket.af_inet, socket.sock_stream) listen_socket.setsockopt(socket.sol_socket, socket.so_keepalive, 1) listen_socket.setsockopt(socket.sol_tcp, socket.tcp_keepidle, 60) listen_socket.setsockopt(socket.sol_tcp, socket.tcp_keepcnt, 4) listen_socket.setsockopt(socket.sol_tcp, socket.tcp_keepintvl, 15) listen_socket.bind((host, port)) listen_socket.listen(1) print 'serving http on port %s ...' % port while true: client_connection, client_address = listen_socket.accept() request = client_connection.recv(1024) print request http_response = """\ http/1.1 200 ok hello, world! """ client_connection.sendall(http_response) client_connection.close()
but every time send keep-alive request, ...
i think mixing application layer http keep-alive , transport layer tcp keep-alive.
http keep-alive used client suggest server underlying tcp connection should kept open further requests client. server might decline , server explicitly closes connection after handled clients request,i.e. finished sending response. apart way server sends response in way makes http keep-alive impossible because length of response unknown , ends end of underlying tcp connection. fix need specify content-length or use chunked transfer encoding.
tcp keep alive instead used detect break of connectivity, i.e. 1 side crashed, router dead or similar. not related http keep-alive @ except similar name. set setsockopt , that's doing. there no such thing keep-alive request can explicitly send in case of tcp keep-alive.
Comments
Post a Comment