Search code examples
pythonapacheserverraspberry-piraspistill

Unable to access pi camera through web browser


I am writing a Python CGI script that I want to run on my laptop's browser. This script will SSH into two Pis, and give the command to take a photo. The server hosting this script is on one of the Pis that I want to SSH into, and that Pi also is acting as an access point for the other Pi and my laptop to connect to (everything is a LAN, not connected to the Internet).

I am successfully able to run this script on my laptop's browser to run simple commands like ls -l on both Pis and print out the results for both on the browser. But, I ultimately want to be able to give the raspistill command to both Pis. When I do this, only the Pi with the server is taking the image, but the other Pi is not. I assume it's because permissions aren't set properly for the server (I tried running the commands as sudo but still no luck). However, if I run the same script on a Python IDLE it works fine. Can somebody help me identify the issue?

Here is my script:

#! /usr/bin/env python3

from pssh import ParallelSSHClient
import cgi

print("Content-Type: text/plain\r\n")
print("\r\n ")

host = ['172.24.1.1','172.24.1.112']
user = 'XXXX'
password = 'XXXX'
client = ParallelSSHClient(host, user, password)

output = client.run_command('raspistill -o test.jpg', sudo=True)

// AMENDMENT:
for line in output['172.24.1.1'].stdout:  // works as well with '172.24.1.112'
    print(line)

AMENDMENT: Apparently, if I output anything from the stdout it works fine. Why is this the case? Is is just waiting for me flush the output or something? I suspect this might be a issue with the pssh package I am using.


Solution

  • After thoroughly reading through the documentation for the pssh module, my problem had to do with the exit codes and how they are handled.

    The documentation about run_command states that:

    function will return after connection and authentication establishment and after commands have been sent to successfully established SSH channels.

    And as a result:

    Because of this, exit codes will not be immediately available even for commands that exit immediately.

    Initially, I was just blindly running the run_command expecting the commands to finish, but it turns out I need to get the exit codes to truly finish the processes the commands are running. The documentations states a couple of ways to do this:

    At least one of

    • Iterating over stdout/stderr to completion
    • Calling client.join(output) is necessary to cause parallel-ssh to wait for commands to finish and be able to gather exit codes.

    This is why in my amendment to the code, where I was outputting from stdout, the commands seemed to work properly.