I have code that connects to a server and sends HTTP GET
requests for a specific file in that server. I can successfully connect to the server and send GET
requests for files, however, when I try to write the content of the received file (they are all .txt
files) to a .txt
file I create, I write the result of the request such as:
HTTP/1.1 200 OK
Date: Wed, 10 Nov 2021 10:17:26 GMT
Server: Apache/2.4.37 (FreeBSD) OpenSSL/1.0.2o-freebsd
Last-Modified: Sun, 01 Aug 1999 17:20:53 GMT
I want to write only the content of the .txt
file received into the file I create.
I have the following code I tried:
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect((host_name, server_port)) # This line initiates the connection
request = ("GET %s HTTP/1.1\r\nHost: %s\r\n\r\n" % (file_name, host_name)).encode()
clientSocket.send(request)
rcvpkt = clientSocket.recv(4096)
received_msg = rcvpkt.decode()
if (received_msg.startswith("HTTP/1.1 404 Not Found")):
print(index + 1, ".", url, "is not found")
else:
received_txt_file = open(only_file_name, 'w')
received_txt_file.write(received_msg)
For instance: when host_name = www.textfiles.com/100/balls.txt
, file_name = /100/balls.txt
, the resulting .txt
file is as follows:
HTTP/1.1 200 OK
Date: Wed, 10 Nov 2021 10:17:26 GMT
Server: Apache/2.4.37 (FreeBSD) OpenSSL/1.0.2o-freebsd
Last-Modified: Sun, 01 Aug 1999 17:20:53 GMT
ETag: "b1d-35109effca740"
Accept-Ranges: bytes
Content-Length: 2845
Content-Type: text/plain
1990 July 12 at 11:17 EDT
To: David Walker
FROM: Jeff Sharpe
...
Additionally, as you can see, I am specifying the size of the received message as 4096 bytes, however, there can be cases where the file received can have 100,000 bytes as well. What can I do to make it efficient? If I write a relatively large number, such as 500,000 bytes, wouldn't it create a problem for small files that have 2000 bytes?