I am trying to learn creating the android application using python. I am going through the tutorials.
when I am running the command :
adb forward tcp:9999 tcp:4321
set AP_PORT=9999
and then running the code,
import android
droid = android.Android()
droid.makeToast("Hello from my computer!")
It gives me error as :
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
droid.makeToast("Hello from my computer!")
File "C:\Python27\lib\site-packages\android.py", line 58, in rpc_call
return self._rpc(name, *args)
File "C:\Python27\lib\site-packages\android.py", line 47, in _rpc
response = self.client.readline()
File "C:\Python27\lib\socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10053] An established connection was aborted by the software in your host machine
how can I solve the problem?
Related
I am trying to write a script for termux. And i need to use psutil with python. And it's working fine. But every time i've tried to run the script it's giving me some errors, which not effects my code.
I've tried to use "try", "except" to catch the error. But it doesn’t work.
Note: See the last line on the error message. Script working fine. If you have other modules or solution to provide, remember 'I can't use os.kill on my script'.
Code:
try:
import psutil, os, signal
except Exception as e:
pass
print ("killing python")
proc = psutil.Process(os.getpid())
proc.send_signal(signal.SIGTERM)
Error + output :
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 110, in wrapper
return cache[key]
KeyError: (('/proc',), frozenset())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 202, in <module>
scputimes = set_scputimes_ntuple("/proc")
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 112, in wrapper
ret = cache[key] = fun(*args, **kwargs)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 184, in set_scputimes_ntuple
with open_binary('%s/stat' % procfs_path) as f:
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 138, in open_binary
return open(fname, "rb", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/stat'
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 110, in wrapper
return cache[key]
KeyError: (('/proc',), frozenset())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/__init__.py", line 1435, in <module>
_last_cpu_times = cpu_times()
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/__init__.py", line 1429, in cpu_times
return _psplatform.cpu_times()
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 301, in cpu_times
set_scputimes_ntuple(procfs_path)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 112, in wrapper
ret = cache[key] = fun(*args, **kwargs)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 184, in set_scputimes_ntuple
with open_binary('%s/stat' % procfs_path) as f:
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 138, in open_binary
return open(fname, "rb", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/stat'
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 110, in wrapper
return cache[key]
KeyError: (('/proc',), frozenset())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/__init__.py", line 1442, in <module>
_last_per_cpu_times = cpu_times(percpu=True)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/__init__.py", line 1431, in cpu_times
return _psplatform.per_cpu_times()
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 314, in per_cpu_times
set_scputimes_ntuple(procfs_path)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 112, in wrapper
ret = cache[key] = fun(*args, **kwargs)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 184, in set_scputimes_ntuple
with open_binary('%s/stat' % procfs_path) as f:
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 138, in open_binary
return open(fname, "rb", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/stat'
killing python
Terminated
Today I had the very same issue when importing psutil in termux on android. The solution / workaround that Mr. lindroid found is helping to avoid that annoying error message. Thanks for sharing!
I want to propose a small improvement:
import os,sys
sys.stderr = open(os.devnull, "w")
try:
import psutil
finally:
sys.stderr = sys.__stderr__
This way, all exceptions which actually prevent the "import psutil" line from execution will get shown. just in case there is a real problem with psutil (i.e. dependency module missing).
Do you know why the error posted originally cannot be caught by a normal try/except statement? Is it raised in a second thread maybe? I already tried manipulating the Thread class to handle the error but it didn't work. What else could be a reason?
If i want to ignore the error messages. I can use following code, because it doesn’t interfere with my actual program.
import os, signal, sys
# set stderr to dev/null
sys.stderr = open(os.devnull, "w")
import psutil
# after importing, set stderr to original
sys.stderr = sys.__stderr__
print ("killing python")
proc = psutil.Process(os.getpid())
proc.send_signal(signal.SIGTERM)
Output :
killing python
Terminated
It can also be handled in a proper way as #TheoRet mentioned:
import os,sys
sys.stderr = open(os.devnull, "w")
try:
import psutil
#except:
#handle module not found
finally:
sys.stderr = sys.__stderr__
This way, all exceptions which actually prevent the "import psutil" line from execution will get shown. just in case there is a real problem with psutil (i.e. dependency module missing).
I am trying to use the MavenToAndroidAnt Python3 to SMACK 4.1.1, but I am getting some errors
I have installed setuptools-15.0b1, python-gnupg-master, httplib2-master
I can't post image yet... i will simulate my cmd prompt
c:\Windows\system32>cd C:\lib
C:\lib>python getMavenArtifactsNg.py -f artifacts.csv -p C:\eclipse\Works\TEsteX
mPP
Downloading smack-android-4.1.1.jar to C:\eclipse\Works\TEsteXmPP/libs/
Downloading smack-android-4.1.1.jar.asc to C:\eclipse\Works\TEsteXmPP/libs/
Downloading smack-android-4.1.1-sources.jar to C:\eclipse\Works\TEsteXmPP/libs-sources/
Traceback (most recent call last):
File "getMavenArtifactsNg.py", line 204, in
a.installIn(project)
File "getMavenArtifactsNg.py", line 147, in installIn
if self.verifySignature(jarSigDest, jarDest):
File "getMavenArtifactsNg.py", line 158, in verifySignature
gpg = gnupg.GPG()
File "c:\python34\lib\site-packages\gnupg-unknown-py3.4.egg\gnupg\gnupg.py", l
ine 125, in init
ignore_homedir_permissions=ignore_homedir_permissions,
File "c:\python34\lib\site-packages\gnupg-unknown-py3.4.egg\gnupg_meta.py", l
ine 176, in init
self.binary = _util._find_binary(binary)
File "c:\python34\lib\site-packages\gnupg-unknown-py3.4.egg\gnupg_util.py", l
ine 427, in _find_binary
raise RuntimeError("GnuPG is not installed!")
RuntimeError: GnuPG is not installed!
C:\lib>
____________ artifacts.csv file
org.igniterealtime.smack,smack-android,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-android-extensions,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-core,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-tcp,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-extensions,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-experimental,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-resolver-minidns,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-sasl-provided,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.jxmpp,jxmpp-core,0.4.2-beta1,1357B01865B2503C18453D208CAC2A9678548E35
org.jxmpp,jxmpp-util-cache,0.4.2-beta1,1357B01865B2503C18453D208CAC2A9678548E35
de.measite.minidns,minidns,0.1.1,4677EF84C286721DA33C09C98D2028BA8AF1E192
EDIT>>>
C:\lib>python getMavenArtifactsNg.py -f artifacts.csv -p C:\eclipse\Works\TEsteX
mPP
Downloading smack-android-4.1.1.jar to C:\eclipse\Works\TEsteXmPP/libs/
Downloading smack-android-4.1.1.jar.asc to C:\eclipse\Works\TEsteXmPP/libs/
Downloading smack-android-4.1.1-sources.jar to C:\eclipse\Works\TEsteXmPP/libs-s
ources/
Exception in thread Thread-8:
Traceback (most recent call last):
File "c:\python34\lib\threading.py", line 920, in _bootstrap_inner
self.run()
File "c:\python34\lib\threading.py", line 868, in run
self._target(*self._args, **self._kwargs)
File "c:\python34\lib\site-packages\gnupg.py", line 753, in _read_response
result.handle_status(keyword, value)
File "c:\python34\lib\site-packages\gnupg.py", line 284, in handle_status
raise ValueError("Unknown status message: %r" % key)
ValueError: Unknown status message: 'NEWSIG'
Traceback (most recent call last):
File "getMavenArtifactsNg.py", line 204, in
a.installIn(project)
File "getMavenArtifactsNg.py", line 150, in installIn
raise Exception("Could not verify signature for " + jarDest)
Exception: Could not verify signature for C:\eclipse\Works\TEsteXmPP/libs/smack-
android-4.1.1.jar
<
I am using Windows 8.1 64X, eclipse SDK 4.2.2 JUNO + ADT
I really want to study the XMPP Smack 4.1.1 documentation, please help me and sorry my english.
New to android and wanted to do a little debugging with the systrace tool however I am unable to run the tool on the emulator (tried running the python script with python 2.0.1, 2.7.x, 3.x) as well as using ADT.
However they all produce different errors:
Developers | Systrace claims that it requires 2.x so using Python 2.x and executing python command systrace.py --help, I get:
C:\Development\Android\sdk\platform-tools\systrace>python systrace.py --help
Traceback (most recent call last):
File "systrace.py", line 286, in <module>
main()
File "systrace.py", line 60, in main
device_sdk_version = get_device_sdk_version()
File "systrace.py", line 44, in get_device_sdk_version
stderr=subprocess.PIPE)
File "C:\Reuben\Softwares\Python 2.7.6\Lib\subprocess.py", line 709, in __init__
errread, errwrite)
File "C:\Reuben\Softwares\Python 2.7.6\Lib\subprocess.py", line 957, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
C:\Development\Android\sdk\platform-tools\systrace>python systrace.py --help
File "systrace.py", line 135
css = '\n'.join(linked_css_tag % (os.path.join(src_dir, f)) for f in css_files)
^
SyntaxError: invalid syntax
But with Admin permission:
C:\Development\Android\sdk\platform-tools\systrace>systrace.py --help
File "C:\Development\Android\sdk\platform-tools\systrace\systrace.py", line 135
css = '\n'.join(linked_css_tag % (os.path.join(src_dir, f)) for f in css_files)
^
SyntaxError: invalid syntax
With ADT settings
Error dialog
Other Relevant Information:
Enabled: Android Debugging, Profile GPU rendering
Enable OpenGL traces: Systrace (Graphics)
Enable traces: Graphics,Input
Tried Other solution but got "mount: Device or resource busy"
I wrote only this 3 lines in python using SL4A:
import android
droid = android.Android()
droid.makeToast(u"ascc4r")
When this code runs, I get the following error:
pydev debugger: starting
Traceback (most recent call last):
File "C:\Users\Tibi\Desktop\adt-bundle-windows-x86_64-20130917\eclipse\plugins \org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1446, in <module>
debugger.run(setup['file'], None, None)
File "C:\Users\Tibi\Desktop\adt-bundle-windows-x86_64-20130917\eclipse\plugins org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1092, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "C:\Users\Tibi\workspace\26\src\26module.py", line 7, in <module>
droid = android.Android()
File "C:\Python26\lib\android.py", line 34, in __init__
self.conn = socket.create_connection(addr)
File "C:\Python26\lib\socket.py", line 547, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
Environment setup:
- Python 2.6.6
- set ap_port=9999
- adb forward tcp:9999 tcp:xxxx (xxxx where I started the server on the phone)
My android.py is in the Python/Lib folder.
Update:
I tried this 3 instruction in CMD and it's work, making a Toast. So I think the fault is in the ADT bundle, or Eclipse Python plugin.
What is this Errno 11001?
What is this Errno 11001?
I don't know about that error, but I suggest to use the correct syntax which is
import android
droid = android.Android()
droid.makeToast('my text to print should be inside the quotes')
see also the API Overview
Without emulator, is there a way to execute the scripts. as any other normal py code?
Tried to port, but /me gets :
droid = android.Android()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/android.py", line 34, in __init__
self.conn = socket.create_connection(addr)
File "/usr/lib/python2.6/socket.py", line 547, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
Yes, you can use a remote connection to your phone and execute the script local, see RemoteControl in the SL4A Wiki.