2

I have a headless Pi Zero that's connected to the internet through the USB ethernet gadget from my computer.

Through SSH (with Powershell), I'm running a Python3 script on the Pi which will start an HTTP server (using Python's native http.server and socketserver library). The Pi will put a local HTML file index.html that's in its SD card online for me to view from my PC by going to raspberrypi.local:8000.

The index.html file is supposed to contain some text and an image. When I run the server on my Pi and view the index.html file through raspberrypi.local:8000, I can see the text just fine. However, the image doesn't appear (just a broken image placeholder).

Here is the code for my index.html file:

<html>
<head>
<title>Pi Cam</title>
</head>
<body style="background-color:#868167;"></body>
<body>
<h1 style="font-family:Calibri" style="text-align:center">Here is a photo of a dog!</h1>
<img src="/focustest.jpg">
</body>
</html>

I'm not sure why I can't view the image. The image is located in the same directory as the index file, but it's not working. Does anyone know what's going on?

My Python code is:

import picamera
import logging
from http import server
import _thread
from ctypes import *
import os
import http.server
import socketserver
import time

camera = picamera.PiCamera()
camera.start_preview()
time.sleep(1)
camera.capture('focustest.jpg')
time.sleep(1)
camera.stop_preview()
camera.close()
print('Image captured. Starting server... ')
print('')

#==============================================================================

try:
    PORT = 8000
    class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
        def do_GET(self):
            self.path = 'index.html'
            return http.server.SimpleHTTPRequestHandler.do_GET(self)
    
    Handler = MyHttpRequestHandler
    
    socketserver.TCPServer.allow_reuse_address = True
    
    with socketserver.TCPServer(("", PORT), Handler) as httpd:
        print("HTTP Server Serving at Port", PORT)
        httpd.serve_forever()

except KeyboardInterrupt:
    httpd.shutdown()
    httpd.socket.close()
    print('Server shut down.')
7
  • what do you see when you navigate to raspberrypi.local:8000/focustest.jpg Jan 31, 2021 at 22:04
  • @JaromandaX Hmm, strangely, I see the index file! (but still without a picture)
    – F16Falcon
    Jan 31, 2021 at 22:16
  • 1
    Guessing src="/focustest.jpg" -> src="focustest.jpg" or src="/home/pi/focustest.jpg"
    – CoderMike
    Jan 31, 2021 at 22:19
  • The Pythin server code is obviously sending index.html for every request Jan 31, 2021 at 22:24
  • 1
    I don't know pythin very well, but I can see it always sends index.html for every request ... self.path = 'index.html' Jan 31, 2021 at 22:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.