0

I have the following python code where I want to get some parts of the file in the server with HTTP Range request. I am defining lower_endpoint and upper_endpoint. What I want to achieve is get bytes from lower_endpoint to upper_endpoint in the content part of the http partial content response only.

The original content of the file is as follows:

www.cs.bilkent.edu.tr/file.txt
www.cs.bilkent.edu.tr/folder2/temp.txt
www.textfiles.com/100/balls.txt
www.cs.bilkent.edu.tr/~cs421/fall21/project1/bilkent.txt
www.textfiles.com/games/arcana.txt
www.cs.bilkent.edu.tr/~cs421/cs421/abc.txt
www.cs.bilkent.edu.tr/~cs421/fall21/project1/files/numbers.txt
www.cs.bilkent.edu.tr/~cs421/fall21/project1/files/decrypted_file_1.txt
www.textfiles.com/100/captmidn.txt

For instance, when I set the lower_endpoint = 0 and upper_endpoint = 200, I want to get the bytes starting with "www.cs.bilkent.edu.tr/file.txt" until 200 bytes.

However, when I run the code with those values I get the following

HTTP/1.1 206 Partial Content
Date: Sun, 19 Dec 2021 07:04:52 GMT
Server: Apache/2.4.25 (FreeBSD) OpenSSL/1.0.2u-freebsd PHP/7.4.15
Last-Modified: Sat, 06 Nov 2021 11:11:25 GMT
ETag: "197-5d01cd2175dc5"
Accept-Ranges: bytes
Content-Length: 201
Conten

I think, the bytes are starting from the beginning of the Header of the response.

The code is below:

import socket


server_port = 80
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host_name = "www.cs.bilkent.edu.tr"
clientSocket.connect((host_name, server_port))  # This line initiates the connection
file_name = "/~cs421/fall21/project1/index1.txt"
lower_endpoint = 0
upper_endpoint = 200
request = ("GET %s HTTP/1.1\r\nHost: %s\r\nRange: bytes=%s-%s\r\n\r\n" % (
                    file_name, host_name, lower_endpoint, upper_endpoint
                )).encode()

power = 1
while power < upper_endpoint - lower_endpoint:
    power *= 2

clientSocket.send(request)
rcvpkt = clientSocket.recv(power)
rcv_str = rcvpkt.decode()

print(rcv_str)

Please do not recommend using third-party HTTP client libraries.

0 Answers0