linux上调试python in android
16lz
2021-01-24
linux上调试python in android
2 #
3 # LicensedundertheApacheLicense,Version2.0(the"License");youmaynot
4 # usethisfileexceptincompliancewiththeLicense.Youmayobtainacopyof
5 # theLicenseat
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unlessrequiredbyapplicablelaworagreedtoinwriting,software
10 # distributedundertheLicenseisdistributedonan"ASIS"BASIS,WITHOUT
11 # WARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.Seethe
12 # Licenseforthespecificlanguagegoverningpermissionsandlimitationsunder
13 # theLicense.
14
15 __author__ = ' DamonKohler<damonkohler@gmail.com> '
16
17 import collections
18 import json
19 import os
20 import socket
21 import sys
22
23 PORT = os.environ.get( ' AP_PORT ' )
24 HOST = os.environ.get( ' AP_HOST ' )
25 HANDSHAKE = os.environ.get( ' AP_HANDSHAKE ' )
26 Result = collections.namedtuple( ' Result ' , ' id,result,error ' )
27
28
29 class Android(object):
30
31 def __init__ (self,addr = None):
32 if addr is None:
33 addr = HOST,PORT
34 self.conn = socket.create_connection(addr)
35 self.client = self.conn.makefile()
36 self.id = 0
37 if HANDSHAKE is not None:
38 self._authenticate(HANDSHAKE)
39
40 def _rpc(self,method, * args):
41 data = { ' id ' :self.id,
42 ' method ' :method,
43 ' params ' :args}
44 request = json.dumps(data)
45 self.client.write(request + ' \n ' )
46 self.client.flush()
47 response = self.client.readline()
48 self.id += 1
49 result = json.loads(response)
50 if result[ ' error ' ] is not None:
51 print result[ ' error ' ]
52 # namedtupledoesn'tworkwithunicodekeys.
53 return Result(id = result[ ' id ' ],result = result[ ' result ' ],
54 error = result[ ' error ' ],)
55
56 def __getattr__ (self,name):
57 def rpc_call( * args):
58 return self._rpc(name, * args)
59 return rpc_call
.首先在Android上开启 SL4A 进入 Interpreter界面start Server.选择private usb连接,这时SL4A开启服务器,比如:127.0.0.1:40122
.在PC中将android.py (=>http://android-scripting.googlecode.com/hg/python/ase/android.py)拷贝到python安装目录或当前目录下
2 #
3 # LicensedundertheApacheLicense,Version2.0(the"License");youmaynot
4 # usethisfileexceptincompliancewiththeLicense.Youmayobtainacopyof
5 # theLicenseat
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unlessrequiredbyapplicablelaworagreedtoinwriting,software
10 # distributedundertheLicenseisdistributedonan"ASIS"BASIS,WITHOUT
11 # WARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.Seethe
12 # Licenseforthespecificlanguagegoverningpermissionsandlimitationsunder
13 # theLicense.
14
15 __author__ = ' DamonKohler<damonkohler@gmail.com> '
16
17 import collections
18 import json
19 import os
20 import socket
21 import sys
22
23 PORT = os.environ.get( ' AP_PORT ' )
24 HOST = os.environ.get( ' AP_HOST ' )
25 HANDSHAKE = os.environ.get( ' AP_HANDSHAKE ' )
26 Result = collections.namedtuple( ' Result ' , ' id,result,error ' )
27
28
29 class Android(object):
30
31 def __init__ (self,addr = None):
32 if addr is None:
33 addr = HOST,PORT
34 self.conn = socket.create_connection(addr)
35 self.client = self.conn.makefile()
36 self.id = 0
37 if HANDSHAKE is not None:
38 self._authenticate(HANDSHAKE)
39
40 def _rpc(self,method, * args):
41 data = { ' id ' :self.id,
42 ' method ' :method,
43 ' params ' :args}
44 request = json.dumps(data)
45 self.client.write(request + ' \n ' )
46 self.client.flush()
47 response = self.client.readline()
48 self.id += 1
49 result = json.loads(response)
50 if result[ ' error ' ] is not None:
51 print result[ ' error ' ]
52 # namedtupledoesn'tworkwithunicodekeys.
53 return Result(id = result[ ' id ' ],result = result[ ' result ' ],
54 error = result[ ' error ' ],)
55
56 def __getattr__ (self,name):
57 def rpc_call( * args):
58 return self._rpc(name, * args)
59 return rpc_call
.在shell中导入环境变量 $export AP_HOST=127.0.0.1 $export AP_PORT=9999(可以写入~/.bashrc中;$source ~/.bashrc)
.$adb forward tcp:9999 tcp:40122 #(android上开的端口号)
.现在可以开启python了
boat@boat-K40AB:~/work/android/python$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import android
>>> droid = android.Android()
>>> droid.makeToast("Hello from my computer!")
Result(id=0, result=None, error=None)
更多相关文章
- Android的 Activity生死周期
- android 屏幕保护
- Android动态获取json解析后显示到RecyclerView
- android TextView 如何动态获取宽度
- Android发送dtmf键盘事件(模拟通话界面键盘事件)
- Android(安卓)Studio Fragment问题与解决
- Android(安卓)NDK编译常见错误及解决方案
- Sina微博的开发心得-1 logo界面
- Android(安卓)4.2.2 ScreenLock上Music远程播放控制界面的创建流