0
        result = s.recv(8192)
        num_bytes_recv += len(result)
        index = result.find('\r\n\r\n')
        result_wo_header = result[index+4:]
        data = ""
        with open(file_name, 'wb') as f:
            data+=result_wo_header
            while True:
                print "number of byte recieve : ",num_bytes_recv
                result = s.recv(8192)
                print result
                if not result: 
                    break
                num_bytes_recv+=len(result)
                data+=result
            f.write(data)   
        print "download complete"
        s.close()

Im trying to download any kinds of file from a given url, the code work perfectly until it reach the end or the last chunk of data, it stuck there up to 10 second. Am I doing anything wrong here? up to 5~10 second from last "number of byte recieve : " to "download complete" please help!!

Madwolf
  • 51
  • 2
  • Use `Content-Length` response header to know how much data you need to read from the socket. See http://stackoverflow.com/a/15995101/2644759. – Philip Tzou Oct 11 '16 at 20:18
  • Are you using `HTTP/1.0` or `HTTP/1.1` protocol? 1.1 defaults to keeping the connection open so it can be reused for multiple requests, 1.0 defaults to closing the connection when the download is done. If you use 1.1 you have to send `Connection: close` to make it close the connection at the end. – Barmar Oct 11 '16 at 20:38
  • Hey Barmar thank you so much, solved my problem perfectly. – Madwolf Oct 12 '16 at 08:47

0 Answers0