python - server is receiving two requests on single request sent by client -
i have written web server in python sends response "website coming soon!" client (client web-browser), problem when client sends single request like:
http://localhost:13555
the server recieving 2 requests are:
1st request:
get / http/1.1 host: localhost:13555 connection: keep-alive accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0 .8 user-agent: mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.81 safari/537.36 accept-encoding: gzip, deflate, sdch accept-language: en-us,en;q=0.8
2nd request:
get /favicon.ico http/1.1 host: localhost:13555 connection: keep-alive user-agent: mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.81 safari/537.36 accept: */* referer: http://localhost:13555/ accept-encoding: gzip, deflate, sdch accept-language: en-us,en;q=0.8
but want 1st request received @ server because when doing operation on client-request 2nd request creating problems, cannot use builtin libraries write web-server, please solve problem.
the server code is:
import socket import re host = "localhost" port = 13555 listen_socket = socket.socket(socket.af_inet, socket.sock_stream) listen_socket.setsockopt(socket.sol_socket, socket.so_reuseaddr, 1) 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(2048) print(request) response = "website coming soon!" http_response = "http/1.1 200 ok\n"+"content-type: text/html\n"+"\n"+"<html><body>"+response+"</body></html>\n" client_connection.sendall(http_response) client_connection.close()
web browsers automatically request favicon (the little image on left of url).
an easy way deal (without parsing request or setting router) see if request contains substring "favicon"
, not robust.
Comments
Post a Comment