建立tcp桥接,并编写可远程调试的Python脚本时出现下面错误:
C:\Documents and Settings\lijt>adb forward tcp:9999 tcp:43785
C:\Documents and Settings\lijt>python
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import android
>>> droid=android.Android()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\installFiles\python265\lib\site-packages\android.py", line 34, in __i
nit__
self.conn = socket.create_connection(addr)
File "D:\installFiles\python265\lib\socket.py", line 500, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
这个错误的原因是因为域名解析出错了,我在环境变量中加入AP_PORT 值为9999,然后在cmd中输入: set AP_PORT=9999回车
adb forward tcp:9999 tcp:43785 回车
键入python
键入 import android
droid=android.Android()
droid.makeToast('Hello World!')
这样就正确了,大家有不同见解,欢迎提出!
附上android.py的源码:
# Copyright (C) 2009 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may not # use this file except in compliance with the License. You may obtain a copy of # the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations under # the License. __author__ = 'Damon Kohler <damonkohler@gmail.com>' import collections import json import os import socket import sys PORT = os.environ.get('AP_PORT') HOST = os.environ.get('AP_HOST') HANDSHAKE = os.environ.get('AP_HANDSHAKE') Result = collections.namedtuple('Result', 'id,result,error') class Android(object): def __init__(self, addr=None): if addr is None: addr = HOST, PORT self.conn = socket.create_connection(addr) self.client = self.conn.makefile() self.id = 0 if HANDSHAKE is not None: self._authenticate(HANDSHAKE) def _rpc(self, method, *args): data = {'id': self.id, 'method': method, 'params': args} request = json.dumps(data) self.client.write(request+'\n') self.client.flush() response = self.client.readline() self.id += 1 result = json.loads(response) if result['error'] is not None: print result['error'] # namedtuple doesn't work with unicode keys. return Result(id=result['id'], result=result['result'], error=result['error'], ) def __getattr__(self, name): def rpc_call(*args): return self._rpc(name, *args) return rpc_call