Note that there are some explanatory texts on larger screens.

plurals
  1. POWeb server in Python 2.7: Browser not displaying page
    primarykey
    data
    text
    <p>I am building a web server in python using the select() function - I/O multiplexing. I am able to connect to multiple clients which in my case are web browsers (safari, chrome, firefox) and accept each clients HTTP 1.1 GET requests. Once i receive the request I return the html page content to the the browser where the html page is displayed.</p> <p>The problem i am getting is when i try to keep the connection open for a while. I realized that i am not able to display anything in the browser until i close the connection using fd.close().</p> <p>Here is the function i am using to accept and respond to the browser request. The problem is after i use fd.sendall(), i dont want to close the connection but the page wont display until i do. Please help! Any help or suggestion is appreciated.. </p> <pre><code>def handleConnectedSocket(): try: recvIsComplete = False rcvdStr = '' line1 = "HTTP/1.1 200 OK\r\n" line2 = "Server: Apache/1.3.12 (Unix)\r\n" line3 = "Content-Type: text/html\r\n" # Alternately, "Content-Type: image/jpg\r\n" line4 = "\r\n" line1PageNotFound = "HTTP/1.1 404 Not Found\r\n" ConnectionClose = "Connection: close\r\n" while not recvIsComplete: rcvdStr = fd.recv( 1024 ) if rcvdStr!= "" : # look for the string that contains the html page recvIsComplete = True RequestedFile = "" start = rcvdStr.find('/') + 1 end = rcvdStr.find(' ', start) RequestedFile = rcvdStr[start:end] #requested page in the form of xyz.html try: FiletoRead = file(RequestedFile , 'r') except: FiletoRead = file('PageNotFound.html' , 'r') response = FiletoRead.read() request_dict[fd].append(line1PageNotFound + line2 + ConnectionClose + line4) fd.sendall( line1PageNotFound + line2 + line3 + ConnectionClose + line4 + response ) # fd.close() &lt;--- DONT WANT TO USE THIS else: response = FiletoRead.read() request_dict[fd].append(line1 + line2 + line3 + ConnectionClose + line4 + response) fd.sendall(line1 + line2 + line3 + line4 + response) # fd.close() &lt;--- DONT WANT TO USE THIS else: recvIsComplete = True #Remove messages from dictionary del request_dict[fd] fd.close() </code></pre> <p>The client (browser) request is in HTTP 1.1 form as shown:</p> <pre><code>GET /Test.html HTTP/1.1 Host: 127.0.0.1:22222 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8) AppleWebKit/536.25 (KHTML, like Gecko) Version/6.0 Safari/536.25 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us Accept-Encoding: gzip, deflate Connection: keep-alive </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload