Connecting to the ftp server on android gives issue - android

I have the android FTPserver app running on my android mobile under Tethering mode. I connected to my mobiles network from my laptop and tried to connect to mobile via FTP to access few files. I wrote a python code that uses the ftp connection to transfer files but I am unable to perform simple connection.
I keep getting this error
in windows:
server.gaierror: [Errno 11004] getadderinfo failed
in linux:
socket.gaierror: [Errno -2] Name or service not known
I searched so many places but never found a proper solution. All I could findout was people suggesting to check the firewall. I checked the firewall settings and everything and still I get the problem.
When I use a client software (filezilla) it does get connected.
I am a newbie to python could someone possibly tell me where I am going wrong?
here is my simple connecting code:
import os
from ftplib import FTP
ftp = FTP("192.168.43.1,5002")
ftp.login("usrid","pwd")
I am unable to connect to a general ftp test site, Here is the code for that:
from ftplib import FTP
ftp = FTP('ftp.cwi.nl')
ftp.login("anonymous","anonymous#")
ftp.retrlines('LIST')

do this:
ftp = FTP()
ftp.connect("192.168.43.1", 5002)
instead of:
ftp = FTP("192.168.43.1,5002")
because you can't give non-standard port number to the constructor.
Code:
from ftplib import FTP
ftp = FTP()
ftp.connect("192.168.43.1", 5002)
ftp.login("usrid", "pwd")
ftp.dir()
ftp.close()

Related

Python Bluetooth how to send a file to a phone

in my current project it is a requirement to send a file from a windows computer to an android device over bluetooth without anything on the phone other than it's standard state and of course a paired bluetooth connection. i've looked over pybluez and it seemed simple enough to send files between a client and server architecture (and in fact got it sending between my laptop and desktop rather quickly) but I cannot for the life of me find any way to get python to send a file from the computer to android once the connection is established; my attempts have been grabbing the bluetooth mac address like thing from the device like so
nearby_devices = bluetooth.discover_devices(
duration=8, lookup_names=True, flush_cache=True, lookup_class=False)
and then later trying to send the files like so
port = 1
for addr, name in nearby_devices:
bd_addr = addr
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
sock.send("download-app")
sock.close()
Of course with the example script given by the pybluez documentation I can seamlessly send files between a client and a server but I am still stuck without a way to send a file to the selected android device (even if I specify it's address and know it's within range)
You're most of the way there...
As you know, you need something to talk to at the other end of your Bluetooth connection. You just need to replace your custom server with a well-known service (generally one of these options).
In my case, my phone supports the "OBEX Object Push" service, so I just need to connect to that and use a suitable client to talk the right protocol. Fortunately, the combination of PyOBEX and PyBluez does the trick here!
The following code (quickly patched together from PyOBEX and PyBluez samples) runs on my Windows 10, Python 2.7 installation and creates a simple text file on the phone.
from bluetooth import *
from PyOBEX.client import Client
import sys
addr = sys.argv[1]
print("Searching for OBEX service on %s" % addr)
service_matches = find_service(name=b'OBEX Object Push\x00', address = addr )
if len(service_matches) == 0:
print("Couldn't find the service.")
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("Connecting to \"%s\" on %s" % (name, host))
client = Client(host, port)
client.connect()
client.put("test.txt", "Hello world\n")
client.disconnect()
Looks like PyOBEX is a pretty minimal package, though, and isn't Python 3 compatible, so you may have a little porting to do if that's a requirement.
I haven't personally explored it but check out this blog -
http://recolog.blogspot.com/2013/07/transferring-files-via-bluetooth-using.html
The author uses the lightblue package as an API for the Obex protocol and send files over the connection. Now the lightblue package appears to be unmaintained. There are other packages like PyObex (which I could not import for whatever reason) which you could also explore as alternatives but lightblue seems to be the way to go.
I have made a Python 3 port of PyOBEX based on the PyOBEX code on bitbucket. I've tested so far only the client functionalities, but I expect the server to be working fine as well, since most of the compatibility issues with Python 3 were due to struct.pack/struct.unpack binary blobs appended to strings that should have all been tackled.

cannot connect server when is in android phone

I'm working on a google cardboard project (Unity that will run on android phone)
I created a server in the Unity program that opens like this:
TcpListener listener = new TcpListener(IPAddress.Any, port);
listener.Start();
TcpListener client = listener.AcceptTcpClient();
Everything on backgroundworkers and nice things.
Then I connect from another piece of code running in a PC (WPF C# code). If Im running both programs in the PC I can connect flawlessly but when I compile for android and move the server to the phone, I cannot connect to it from the PC
When I try to perform:
host = Dns.GetHostEntry("192.168.1.8");
I get: Unknown host
The IP is correct (I made the server in the phone show it on screen, and I can ping to it) I think the problem is more related to the thing being in a different machine than to the fat that it is compiled for android but nevertheless.. here you have the full story.
Any help ?
Not sure what it was becauseI moved to UDP messages, and now it works.

Android FTP with 4G

I am trying to connect to an FTP server in an Android application and I believe that I fall in an infinite loop, but can't understand why. The library I am using is Apache Comomns net.
I have the internet permission in manifest :
<uses-permission android:name="android.permission.INTERNET"/>
I am also connecting to the FTP in an AsyncTask.
Here is a sample code which was supposed to work :
// I have other values when I test it of course
FTPClient ftpClient = new FTPClient();
ftpClient.connect("111.111.111.111"); // It stops working here when in 4G
ftpClient.login("user", "mdp");
ftpClient.changeWorkingDirectory("directory");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.logout();
ftpClient.disconnect();
It works on Wifi (and my real application works too in wifi) but when I use my 4G (or 3G) it seems to loop at the line where I try to connect() forever.
I have tryed to understand what there is behind this but it's not clear for me.
How can I connect to my FTP using 4G?
Any help would be appreciated.
It sounds like your network operator may have the FTP port blocked.
To test this you can try to ftp to the same site on a browser in your mobile device - if it works then it point to an issue in your app.
If it does not work then try with a known working FTP site - Tele2 host a test site for example (there are lot of other open FTP sites if you do a quick search):
ftp://speedtest.tele2.net
If you can't access this from your mobile device over 4G then it looks very likely that your operator has FTP, and you should contact them to find out why and what you can do to remove the block.

How to make socket connection from Android Emulator to another pc which is on the same lan

I am very new to Android. Our lan is working in class C IP (192.168.1.x series).
I need to establish socket connection between android emulator(Server) and java eclipse (eclipse-java-juno-SR1) which is acting like a client. As I got a information from "http://developer.android.com" website every Android emulator having 10.0.2.15 (Class A IP) by default. But our LAN is working in Class C.
"PC-1" which is using for Android Emulator is having 192.168.1.50 IP, and
the another one "PC-2" which is using for Eclipse-Java is having 192.168.1.55.
This is the setup which I have.
My work is, When I send a string command (through socket) from Android emulator(runing on PC-1) to Java program(runing on PC-2) then that Java program should send continuous jpg photo to Android emulator through socket.
I tried lot but when I try to work with sockets installed apk application is closing by this error: "Unfortunately, Activity name> is closing"
Please help me out in this...
Thanks in advance :)
Does your Application have the
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
in your Manifest - File ?
You can either insert the above line into the manifest xml,
or you can go to "permissions" add "uses permission" and then add the "Internet" one.
My Emulator has no problems opening a Webview to the internet with this permission,
and I can even access my router in my network through the browser in the emulator, which is on some 192.168... address.
Kind Regards and a Happy New Year,
karlheinz

Problem when connecting to FTP server through android?

I've an UnknownHostException when i used this method for uploading files from ddms:
try {
SimpleFTP ftp = new SimpleFTP();
// Connect to an FTP server on port 21.
ftp.connect("ftp://*******", 21, "*****", "*****");
// Set binary mode.
ftp.bin();
// Change to a new working directory on the FTP server.
ftp.cwd("web");
// Upload some files.
ftp.stor(new File("data/data/com.android/file/contacts"));
// Quit from the FTP server.
ftp.disconnect();
}
catch (IOException e) {
// Jibble.
}
What is the problem for this method? Anyone clarify me.
There are two major problems with using FTP on the android emulator:
The emulator takes hold of a specific port on the host machine (between 5554 and 5584) to access the internet.
See http://developer.android.com/guide/developing/tools/emulator.html
FTP communicates on two ports. The initial port (the one the emulator is using) and a secondary data communication port (usually defined by the FTP client and server).
See http://www.troubleshootingnetworks.com/ftpinfo.html for information on how FTP works.
This means that the initial communication with the FTP server works the way it is intended, but once you are attempting to pass data to / from the server the emulator cannot communicate with the port the FTP server requests because your computer doesn't know what to do with the traffic on that port. See the link above to get a better grasp on FTP communications.
If you want to test FTP on Android you will need to have a device with its own internet connection.
Cursory look at SimpleFTP example suggests that you need to use host name without ftp:// prefix. Also make sure that you include INTERNET permission in manifest.

Categories

Resources