Alright first off I want to point out that no networking libraries can be used. The purpose of what I'm trying to do is learn how http 1.1 headers and data are to be served using string manipulation/conversions. In this case I'm trying to serve a .jpg file with the url:
http://127.0.0.1:8080/01.jpg
My server has already received the GET request and my issue seems to be from either file i/o or the way I'm sending data to the client. I will admit I'm new to python and HTTP so be gentle with me ;). This is done on python 3.4 on ubuntu 14.04.
Relevant Code:
file_data = open(file_directory,'rb')
...
# Other code in between exists
...
# res is a unicode string that contains the headers for the http response
header = bytes(res, 'ascii')
# Send Response header
client_sock.sendall(header)
# Send file data
client_sock.sendall(file_data)
Questions:
Is it expected that the binary read of a .jpg file be:
<_io.BufferedReader name='/home/website/html/01.jpg'>
I've seen other binary values from multiple sources that looked nothing like this. From my understanding data needs to be binary in order for the server to send it to the client over the socket. If this is not correct what am I missing from my file i/o? If this is correct what step am I missing that would allow me to send the data using sendall()?
Given the question above, how does the file_data affect sendall()?
client_sock.sendall(file_data) TypeError: '_io.BufferedReader' does not support the buffer interface
From reading the python3 documentation section 18.1 I see that the parameter for sendall() should be bytes. Does file_data need to be encoded using bytes(xxx, 'idk what encoding') like for headers? using 'ascii' did not work at all.
- Would you advise me to use a byte array to store both the http response header and the file data so that both can be sent in one sendall()? Or is this not necessary?
The output on the browser(Firefox):
The image "http://127.0.0.1:8080/01.jpg" cannot be displayed because it contains erros.
Thank You all. Please let me know if more information is desired.