How to downloading and uploading file on NAO programmatically - nao-robot

I want to download file /recordings/cameras from Robot nao to my computer .
how can I do that ? Give me an example ( script ) please .

You can use Paramiko package for that.
import paramiko
NAO_IP = "put_nao_ip_here"
NAO_USERNAME = "put_nao_username_here"
NAO_PASSWORD = "put_nao_password_here"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(NAO_IP, username=NAO_USERNAME, password=NAO_PASSWORD)
sftp = ssh.open_sftp()
localpath = 'path to save the file locally with filename'
remotepath = '/recordings/cameras/<filename>'
sftp.get(remotepath, localpath)
sftp.close()
ssh.close()

Related

How to read file names from a given path on server using Groovy

I have multiple files on the given FTP path. I want to read the latest file received, for which I need to read all the file names in the given path. I am using Groovy on the OUAF based product, ORMB. Please suggest on how to read the file names. Also, new File() statement is not supported.
Assuming you want to use apache commons-net module you can do this:
#Grab(group='commons-net', module='commons-net', version='3.7')
import org.apache.commons.net.ftp.FTPClient
String hostname = "some-server.some-domain.com"
String username = "someUser"
String password = "somePassword"
String path = "/var/some/path"
println("About to connect....");
new FTPClient().with {
connect hostname
enterLocalPassiveMode()
login username, password
changeWorkingDirectory path
FTPFile latest = listFiles().max { it.timestamp.time }
println("Downloading ${latest.name}..")
File incomingFile = new File( latest.name )
incomingFile.withOutputStream { os -> retrieveFile(latest, os) }
disconnect()
}
println("...Done.");

Flutter - Save file visible to the user

In a Flutter project I create a pdf document. I can save the document in the path of the app.
But the user has no access to it. Alternatively, how can I save the file to another folder where the user sees it?
import 'package:path_provider/path_provider.dart';
Future<void> savePdfDocument() async {
final PdfCreater generatedPdf = PdfCreater(document);
final List<int> generatedPdfDocument = generatedPdf.buildPdf();
final String dir = (await getApplicationDocumentsDirectory()).path;
final String path = '$dir/example.pdf';
final File file = File(path);
file.writeAsBytesSync(generatedPdfDocument);
}
Use downloads_path_provider, on android it will save the file on Downloads.
for IOS, you need to use getApplicationDocumentsDirectoryfrom path_provider and add permissions on info.plist to make the folder visible to the user:
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>UIFileSharingEnabled</key>
<true/>
This works like a charm
String documentsPath = '/storage/emulated/0/Documents/';
if (Platform.isIOS) {
Directory path = await getApplicationDocumentsDirectory();
documentsPath = path.path;
}
Also don't forgot to add this to pubspec
path_provider: 2.0.1
Also, be sure to test on a physical device, iOS emulator won't work.

How to download a file via FTP and save it locally only if it does not exist already?

So, I'm downloading some data files from an ftp server. I need to daily go in and retrieve new files and save them on my pc, but only the new ones.
Code so far:
from ftplib import FTP
import os
ftp = FTP('ftp.example.com')
ftp.login()
ftp.retrlines('LIST')
filenames = ftp.nlst()
for filename in filenames:
if filename not in ['..', '.']:
local_filename = os.path.join('C:\\Financial Data\\', filename)
file = open(local_filename, mode = 'x')
ftp.retrbinary('RETR '+ filename, file.write)
I was thinking of using if not os.path.exists() but I need the os.path.joint for this to work. Using open() with mode = 'x' as above, I get the following err message: "FileExistsError: [Errno 17] File exists"
Is error handling the way to go, or is there a neat trick that I'm missing?
I landed on the following solution:
filenames_ftp = ftp.nlst()
filenames_loc = os.listdir("C:\\Financial Data\\")
filenames = list(set(filenames_ftp) - set(filenames_loc))

Uploading and storing file with Laravel

I am using Laravel illuminate/html and I am trying to upload an image file and store it in /public in the laravel installation folder. I have got the image from the request:
$img = Request::file('img');
How can I store it in the public folder?
Thanks
You could do this in your controller:
Request::file('img')->move(base_path('public/uploads'));
Or if you wish to specify a generic filename or change filename
$newfilename = str_random(32) .time();
$ newfilename = $newfilename. ".". Request::file('img')->guessClientExtension();
Request::file('img')->move(base_path('public/uploads'), $newfilename);`

App Engine Credential issue

I have an app engine app which imports a jar. In that jar I am using
GoogleClientSecrets.load()
to load client_secrets.json file for authentication with BigQuery. Apparently, App Engine does not like me reading a file from some location on my disk when I deploy the app on localhost. I am assuming if I put the credentials in WEBINF folder it will work but haven't tested it but then it would be easy for anyone to access the file. Where is the best place to put credentials and how would one access them from App Engine app?
Thank you for your help!
The suggestions helped to solve the problem when it comes to reading a file. What about writing to a file? I am using FileCredentialStore which stores credential file.
I believe this line is causing a problem:
FileCredentialStore variantStoreCredentialManager = new FileCredentialStore(expectedClientFile,jsonFactory);
and the error is
java.security.AccessControlException: access denied ("java.io.FilePermission" file path "write")
public Bigquery createAuthorizedClient() throws IOException {
Credential authorization = new GoogleCredential();
if ( clientID == null ) {
authorization = createWebAuthenticatedClientCredential();
} else {
String expectedFileLocation = CREDENTIAL_FILE_PATH;
File expectedClientFile = new File(expectedFileLocation);
if ( ! expectedClientFile.exists() ) {
// this is a known issue, the credential store will blow up if the file doesn't exist. So create it with an
// empty json ( { } )
createClientFile(expectedClientFile);
}
FileCredentialStore variantStoreCredentialManager = new FileCredentialStore(expectedClientFile,jsonFactory);
GoogleCredential.Builder credentialBuilder = new GoogleCredential.Builder();
credentialBuilder.setJsonFactory(jsonFactory);
credentialBuilder.setClientSecrets(clientSecrets);
credentialBuilder.setTransport(transport);
authorization = credentialBuilder.build();
boolean loadedSuccessfully = variantStoreCredentialManager.load(clientID,authorization);
if ( ! loadedSuccessfully ) {
authorization = createWebAuthenticatedClientCredential();
variantStoreCredentialManager.store(clientID, authorization);
}
}
return new Bigquery(transport, jsonFactory, authorization);
}
No, contents of /WEB-INF folder is private to application code and is not accessible via HTTP (= servlet container does not honour requests that try to access data in WEB-INF folder).
Use this snippet to read contents of a file inside a /WEB-INF folder:
InputStream is = getServletContext().getResourceAsStream("/WEB-INF/"+filename);
Then read a stream using one of the methods for reading InputStreams.

Resources