I have a python http server implemented which only responds to get request to serve data plots to me remotely showing me what my photovoltaic system is doing at home. I have noticed some failed requests from IP addresses that are not any of my own and would like to lock this down a little to reject connections from computers that are not me specifically.
Example: If there were a way to only respond to GET requests that originate from the MAC address of my iPhone and provide no response otherwise so the service is undetectable by others, that would be ideal.
Question 2: If I exit this script using ctrl-c does this code leave something running or does the daemon take care of stopping the server when I exit the script?
Here is the code I am currently using. Any thoughts, comments, suggestions you might have for locking this thing down would be great!
from http.server import HTTPServer, BaseHTTPRequestHandler, ThreadingHTTPServer
import threading
def load_binary(filename):
with open(filename, 'rb') as file_handle:
return file_handle.read()
class MyRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
os.system('python3 /home/pi/Plot_Generator_0_2.py')
mimetype='image/png'
self.send_response(200, 'OK')
self.send_header('Content-type',mimetype)
self.end_headers()
self.wfile.write(load_binary('plot_image.png'))
if currentOS != "Windows":
server = ThreadingHTTPServer(('192.168.0.122', 5555), MyRequestHandler)
server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()