What is best solution file upload for python server - android

I programming python server and android client.
program's logic is client send multiple file to server.
I first try C/C++ socket server but receive error. so I change python. because server on raspberry pi.
I have to implement file upload, audio streaming. so I think this logic.
1. Client send http request to server
2. When server receives the request, server create tcp socket and listen.
3. Client receives success response, connect to server and file upload.
Audio streaming will implement similar way.
Is it ok to implement this way? or is there a better way?
Please give me a hint how to implement it.

Ignore socket as much as you for small deployments. They are little more complicated to handle.
Now asuming you want to upload a image file or some other file to a python you can use Flask Upload
Next move on to audio, if you have to upload audio from client to server, than there is no need to stream or stuff, just pass appropriate MIME type during upload.
ALLOWED_AUDIO_EXTENSIONS = set(['wav', 'ogg', 'mp3', ])
def audio_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_AUDIO_EXTENSIONS
if file and audio_file(file.filename):
filename = secure_filename(file.filename)
#perform some application logic with audio files and then save them in file system or call boto3 to save on s3
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
Code is modified from flask examples.

Related

How to execute a set of Python scripts at Ubuntu Server from Android Client app and return the result to client app

I am using an Android application as a client and an Ubuntu PC as a server for my Project. I am sending a video file from my android client to server. After that at Server I have to execute a set of Python scripts and then result will be sent as a textual data to Android client.
I have already made an Android app which sends video from android device to the Ubuntu Server which is a Xampp server (suggest any other server if you feel that is better), but now after receiving the file at Server side I want to process it by executing the set of Python scripts automatically at Server and return the result back. Is anyone can help me who can tell How to do this part?
If you owe the server or can modify API, used for uploading video, problem is solvable on the server side. Simply return result of python scripts as response to upload request. For detailed answer, please provide more information about server software.
If you cant't modify the server code or want control execution from Android, I recommend you consider about using ssh for that purpose. As instance, there are some 3rd party libraries, that support ssh communication, such as hierynomus/sshj.
Example of ssh communication:
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("your.server.host");
try {
ssh.authPublickey(System.getProperty("user.name"));
final Session session = ssh.startSession();
try {
final Command cmd = session.exec("python script.py");
String response = IOUtils.readFully(cmd.getInputStream()).toString();
cmd.join(5, TimeUnit.SECONDS);
} finally {
session.close();
}
} finally {
ssh.disconnect();
}

How to send data from an android application (client) to a python flask (backend) remote server

First of all bear with me as I'm new to both android applications and python flask.
I found sample code (java) on the web, which sends json data from an android application to a php web page (http://hmkcode.com/android-send-json-data-to-server/). What I want to do is to use this sample android code to send the data to a remote VPS server and use python flask as backend to receive and save the data .
As far as I understand the first thing that I have to replace is the php-address http://hmkcode.appspot.com/jsonservlet with my server's IP address (99.99.999.99) in the java code,
switch(view.getId()){
case R.id.btnPost:
if(!validate())
Toast.makeText(getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show();
// call AsynTask to perform network operation on separate thread
//new HttpAsyncTask().execute("http://hmkcode.appspot.com/jsonservlet");
new HttpAsyncTask().execute(99.99.999.99);
break;
}
Then I have to add the python flask code which will receive the data.
Concerning flask I found the following code which might apply to my issue,
https://github.com/javierchavez/Android-with-flask-backend/blob/master/Backend/wsgi.py.
Thus my questions are :
1st Is it correct to only replace the php-address with my VPS IP address, or do I also have to add the path to the flask file in my remote's server home directory.
2nd what would be the actual flask code, which will receive the data from the application and save it to the remote server (as far as I understand the python flask file will be saved in the VPS's home directory)
3rd do I also have to take into consideration the password required to access my VPS remote server when modifying the android and flask code?
Thank you in advance
Answering to the first question, it is correct to only specify the IP if the action you want to perform is declared on the root ("/") route of the flask server, otherwise you have to append the path you specify in the #app.route() tag.

Android how to transfer real-time stream videos to a node js server

I'm an android developer but a noob in nodejs.I want to create a live radio appliction so I use android to create the client app and nodejs to create the server.
Step:
Android : MediaRecord to record video and transfer to nodejs server.
Nodejs server : Receive the stream and transfer it to another clients or save it in the database.
I read the MediaRecord apis and found the native way to send stream videos to server.
Socket socket = new Socket(“xxx.xxx.x.xxx”, 8890);
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);
MediaRecorder.setOutputFile(pfd.getFileDescriptor());
But it the NATIVE socket. NOT suit for nodejs server.
I use the Socket.io to establish the socket server and the socket.io-client in android client to connect to the socket server. But it doesn't support to transfer the stream video info from android to server. Then I found socket.io-stream. It supports to transfer the stream info but no any android api. So I want to know what should I do or what should I use to transfer stream videos from Android to a nodejs server? Do I need to estabilsh my own nodejs socket libs to finish this work?
Any help will be appreciated. Thanks anyway.

Is it possible to download a file from server whithout giving the file URL?

I was wondering if it was possible to download a file without giving the specific file URL.
For instance, making an http request and inside the response were the file or the content of the file.
I'm able to download a file already giving the exact URL.
For instance, txt file.
But, what I want next is to protect that URL and make it private.
If there is another way, then please explain me
There is a way to facilitate server-client communication without using http request, referred to as Apache Thrift. To achieve the goal you asked - "Download a file from server without giving the file URL", you can do as follow:
Download Thrift, Build and Install Apache Thrift compiler
You can follow steps on official website.
Writing a .thrift file (For my example, I named it as "test.thrift")
For your example, you can define as below:
service MyServer {
binary getFileFromServer()
}
Then, open you terminal and run
thrift --gen java test.thrift
I'll get a "MyServer.java" file and both server/client sides should include this file for implementing. Assuming your server side is prepared for providing file data as binary. In your Android app, you just need to specify the socket, protocol, transport, host, and port for connecting server. And if you would like to get file from server, just using your Client object to call "getFileFromServer()", then you can get the byte[] of your file. For instance:
public TSocket socket = new TSocket(your_host, your_port);
public TFramedTransport transport = new TFramedTransport(socket);
public TBinaryProtocol protocol = new TBinaryProtocol(transport);
public Client myClient = new Client(protocol);
call a thread to get file from server:
new Thread(new Runnable() {
#Override
public void run(){
// this block should be wrapped with try/catch
transport.open();
byte[] myFileInByteArray = myClient.getFileFromServer();
// your custom function for decoding
decodeByteArrayIntoFile(myFileInByteArray);
transport.close();
}
}).start();
The above is the only way I can think of to download a file from server without giving the file URL, but your still need to connect to your server first. Hope this can help.

android post gzip

Trying to implement gzip on an Android project to reduce client data charges. All devices connect to a WCF webservice and IIS is now sending compressed data back to the devices as expected. Now I need to work out how to post back gzipped xml data modified on the android device.
The device code is as follows
httpsURLConnection.setDoInput(true);
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setConnectTimeout(TIMEOUT);
httpsURLConnection.setReadTimeout(TIMEOUT);
httpsURLConnection.setRequestMethod("POST");
httpsURLConnection.setRequestProperty("Content-Type", "application/xml");
httpsURLConnection.setRequestProperty("Content-Encoding", "gzip);
httpsURLConnection.connect();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new BufferedOutputStream((httpsURLConnection.getOutputStream())));
gzipOutputStream.write(_xmlText.getBytes());
gzipOutputStream.flush();
gzipOutputStream.finish();
gzipOutputStream.close();
Running Wireshark on webserver shows the gzip packets and decompresses them to show the correct data from the device.
The problem is that the WCF web service does not seem to recognise the data - the error is XmlException - The data at the root level is invalid. Line 1, position 1.
Which makes me think that the data is still compressed and WCF cannot handle gzip - which I seem to remember reading about previously. Do I then need to create a message decoder in .net to handle the gzip compression? Was this hopefully addressed in .net 4.5?
Any help with these questions appreciated.

Categories

Resources