Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Connection: close</code> indicates to the browser that you'll tell it when you're done sending data by closing the connection. Since you don't want to do that, you'll probably want to use a different value for <code>Connection</code>, like <a href="https://en.wikipedia.org/wiki/HTTP_persistent_connection" rel="nofollow"><code>Keep-Alive</code></a>. If you use that, though, then you'll also need to send <code>Content-Length</code> or do something else so the browser knows when you're done sending it data.</p> <p>Even if you're not using <code>Keep-Alive</code>, <code>Content-Length</code> is a good thing to send, because it allows the browser to know the current progress in downloading the page. If you have a big file you're sending and don't send <code>Content-Length</code>, the browser can't, say, show a progress bar. <code>Content-Length</code> enables that.</p> <p>So how do you send a <code>Content-Length</code> header? Count up the number of bytes of data you'll send. Turn that into a string and use that as the value. It's that simple. For example:</p> <pre><code># Assuming data is a byte string. # (If you're dealing with a Unicode string, encode it first.) content_length_header = "Content-Length: {0}\r\n".format(len(data)) </code></pre> <hr> <p>Here's some code that's working for me:</p> <pre><code>#!/usr/bin/env python3 import time import socket data = b'''\ HTTP/1.1 200 OK\r\n\ Connection: keep-alive\r\n\ Content-Type: text/html\r\n\ Content-Length: 6\r\n\ \r\n\ Hello!\ ''' def main(server_address=('0.0.0.0', 8000)): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) server.bind(server_address) server.listen(5) while True: try: client, client_address = server.accept() handle_request(client, client_address) except KeyboardInterrupt: break def handle_request(client, address): with client: client.sendall(data) time.sleep(5) # Keep the socket open for a bit longer. client.shutdown(socket.SHUT_RDWR) if __name__ == '__main__': main() </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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