PiCurl

Forums:

Update 2019Dec17-Added dev/null output on wget call so it does not output a result file. Update 2019Dec14- Because there was a lot of additional code from piup.py, I have removed everything not specifically applicable to picurl.py. This significantly reduced the lines in the python script. At one point I was sending my pi information to a server by FTP. Though this worked ok but I really wanted the ability to upload via API. On the host there is a simple php API that checks for each of the GET params and if succesful it writes to a file which can be viewed by the same script. If you pass a read parameter, it will read the file to get the machine information and display it on the web page. Here is the local pi script called picurl.py (python3 picurl.py) which runs via crontab on each of the pi units. crontab -e Add the lines: 0 * * * * python3 /home/pi/bin/picurl.py Python Script Here is an example of a simple php scripted API. Please use caution, there is not much in the way of error checking, validation, etc. **** Use at your own risk. Be sure to add any additional checks to secure and validate the incoming connections to the php API.
<!-- apiscript.php --> <?php #define which devices are allowed to connect $mykey="abc123DEF456"; $hostList=array("device1","device2"); $supausers=array("yoursuperusername"); $devicedir="public://mydevices/"; $tstamp=date("Y/m/d h:i:s a"); $assoc=".dvc"; date_default_timezone_set("America/New_York"); if(isset($_GET['viewdevice']) && isset($_GET['myAPIkey']) && isset($_GET['hostname'])) { if($_GET['ViewDevice']==1 && $_GET['myAPIkey']==$mykey in_array($_GET['hostname'],$hostList)) { $hstfile=$devicedir.$_GET['hostname'].$assoc; if(file_exists($hstfile)) { $tsf = fopen($hstfile, "r") or die("Invalid Device"); $fdat = fread($tsf,filesize($hstfile)); fclose($tsf); die(str_replace("\n","<br>",$fdat)); } else { die("Device has not checked in yet".$hstfile); } } } #incoming API call from device, log and quit if(isset($_GET['myAPIkey']) && isset($_GET['myAPIinit'])&& isset($_GET['mip'])&& isset($_GET['mhst'])&& isset($_GET['mgw'])) { $ctkn=$_GET['myAPIinit']; $stkn=$_GET['myAPIkey']; $mhst=strtolower($_GET['mhst']); $mip=$_GET['mip']; $gwy=$_GET['mgw']; $cr="\n"; $dat=$tstamp.$cr.$mhst.$cr.$mip.$cr.$gwy.$cr; $tokenfile=$devicedir.$mhst.$assoc; if($ctkn=="LogMyDeviceInfo" && $stkn==$mykey && in_array($mhst,$hostList)) { file_prepare_directory($devicedir, FILE_CREATE_DIRECTORY); $file = file_save_data($dat,$tokenfile,FILE_EXISTS_REPLACE); die("Device Registered"); } else { die("Invalid Device"); } } ?> <?php $sur=""; if (isset($_GET['susr'])) { $sur=$_GET['susr']; } if (in_array('supausers', $sur))) { $files = file_scan_directory($devicedir, '/.*\.ts$/'); echo "Devices: ".count($files); ?> <style> li{ margin-bottom:2px; } li.pointer { cursor: pointer; } li:hover { background-color: #ddd; } </style> <div id=silverdiv class="rnd" style="float:left;width:98%;background: linear-gradient(to left, #333, #333 50%, #eee 75%, #333 75%);"> <button class="rnd" onclick="fileDisp()" style="float:none;background-color:blue;color:white;font-size:x-small;cursor:pointer;box-shadow:none" title="Click for files">View Current Devices</button> <div id="lfdiv" class="rnd" style="float:left;color:gray;background-color:silver;#eaeaea:none;width:100%;font-size=xx-small;display:none"> MyDevices<br> <div id="filediv" class="rnd" style="float:left;width:100%;overflow:auto;height:100px;background-color:black;color:white"> <?php $fcnt=0; foreach ($files as $file => $loci_file) { $fcnt++; $furl=$devicedir.$loci_file->name.$assoc; $fname=$loci_file->name; $acd=$fname; $aky= explode("\.",explode("_",$fname)[1])[0] ; $linkurl="?viewdevice=1&hostname=".$acd."&key=".$mykey; $purl="/".$loci_file->name.$assoc."\" target=\"purl\" style=\"height:20px;width:16px;border-radius:2px;font-size:xx-small;border-width:1px;border-color:white;border-style:solid;margin-right:8px;color:black;float:left;font-size:large;background-color:yellow;align-text:center\" title=\"Download\">d</a>"; $ico="<span style=\"height:20px;width:16px;border-radius:2px;font-size:xx-small;border-width:1px;border-color:white;border-style:solid;margin-right:8px;color:black;float:left;font-size:large;background-color:tan;align-text:center\" title=\"File #".$fcnt."\"><b>f</b></span> "; echo $purl."<div class=\"pointer\" onclick=fShow(\"".$linkurl."\") title=\"".$linkurl."\"> ".$ico.substr($fname,0,14)."</div><br>"; } ?> </div> <!-- file viewer --> <iframe id="fviewer" source="" style="width:100%;float:left;overflow:auto;height:100px;background-color:white;margin:4px" class="rnd"> </iframe> <!-- end file viewer --> </div> <!-- end file div --> <script> function fileDisp() { var x = document.getElementById('lfdiv'); if (x.style.display === 'none') { x.style.display = 'block'; } else { x.style.display = 'none'; } } function fShow($pth) { document.getElementById('fviewer').src=$pth; } </script> <?php } //endargs else //no access { ?> <div class="rnd txt"> Access denied<span id="dconphn" style="font-size:large"></span> </div> <?php } ?>