Coolq B Coolq B - 7 months ago 19
Python Question

Python HTTP server, can't use images

What I've done:
I've created a small HTTP server python, using no modules but socket. It manages to start, load HTML and then send it out,

so you can see the HTML file in your browser.

What I'm using:
I'm using Python 3.5.1, I'm also using Socket.

The problem:
Images do not show on the web browser.

Here is the code I have put together:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

HOST = ''
PORT = 8888

asked_url = []

s.bind((HOST, PORT))

s.listen(1)

while True:
conn, addr = s.accept()

request = conn.recv(1024)

html_file = open("page.html", 'rb')
try:
http_response = html_file.read()
conn.sendall(http_response)
conn.sendall(b'<p><small>PyWeb</small></p>')
except:
print("Couldn't send any data! Passing...")
pass
conn.close()


Here is also the HTML code for the page, However it doesn't seem to be the problem:

<script src="https://mcapi.us/scripts/minecraft.js"></script>
<div class="server-status">
My awesome server is currently <span class="server-online"></span>!
</div>
<script>
MinecraftAPI.getServerStatus('SERVER HERE', {
port: 25565 // optional, only if you need a custom port
}, function (err, status) {
if (err) {
return document.querySelector('.server-status').innerHTML = 'Error loading status';
}

// you can change these to your own message!
document.querySelector('.server-online').innerHTML = status.online ? 'up' : 'down';
});
</script>

<img src="test.png" alt="Test Image" style="width:304px;height:228px;">


What the HTML code does:
Shows text and an image.

If you need more detail just ask.

Answer

Where is test.png located? Looks like your HTML is having an issue finding the file. I ran your code locally and was able to see an image when I changed to image tag to source an image from a url.

I checked chrome dev tools first and noticed the image tag rendered fine.

I changed the image tag to (random image from google image search):

`<img src="http://emojipedia-us.s3.amazonaws.com/cache/f8/69/f869f6512b0d7187f4e475fc9aa7f250.png" alt="Test Image">`

And it showed up fine.