0

I'm trying to update a page of mine which currently does not have a way to refresh the version number that is read from a file because the page is only generated once by a Python script. I decided to convert the page to a static page rather than a generated one and try to just read the information using JavaScript. The problem is that two of the files I need to read are /proc/cpuinfo and /proc/device-tree/model. The files are server-side.

I can't seem to find any information on using FileReader with just a file path, and it seems to rely on the "file" input to provide a File object. I looked into using XMLHttpRequest, but would that even be able to read a file that is not under /var/www?

The reason the Python script was able to read the files is because it is a cgi script and has sudo permissions.

Do I have any options to do this with a static HTML page? Or will I need to keep the page generator script since those two files live outside the /var/www directory?

Darin Beaudreau
  • 375
  • 7
  • 30
  • Perhaps you can symlink them under `/var/www`? If the server is set up to follow symlinks, `XMLHttpRequest` should be able to fetch them – that other guy Apr 26 '19 at 23:31

2 Answers2

4

Your static page cannot access the server using JavaScript directly. That would be a huge security hole. Because I can run JavaScript myself on your page simply by opening the developer tools. Something has to run on the server in response to a request from your webpage, and return that information.

Todd Chaffee
  • 6,754
  • 32
  • 41
2

Instead of generating a full page with the version number embedded in the page with phyton, generate with phyton a response containing only the version number and than require that from javascript with XMLHttpRequest (see ajax)

  • This is probably the direction I was going to take because I didn't expect I'd be able to access **/proc** from JavaScript, but how do I return information from the Python script to the ajax callback function? – Darin Beaudreau Apr 29 '19 at 13:10
  • Through some searching, it seems like the only way to handle GET requests and return data back to the calling JavaScript is to use a framework like Flask, and I don't want to have to implement that just to retrieve 2 lines of information... there has to be a simpler way. – Darin Beaudreau Apr 29 '19 at 13:31
  • 1
    @DarinBeaudreau you shouldn't need an entire framework just to respond to a GET request. Example at this answer. https://stackoverflow.com/questions/19434947/python-respond-to-http-request – Todd Chaffee Apr 29 '19 at 15:36