用于在Android中运行Python CGI脚本。基本上你需要一个能够在Android平台上运行CGI脚本的web服务器,但是,我没有找到。您需要toy CGI脚本来适应Cherrypy web服务器,它可以在Android上运行。
步骤如下:我们首先需要下载SL4A(r4)软件
Android(2.3)模拟器。它可以通过你的Android浏览器
去SL4A网站。现在在模拟器中安装软件。
然后从同一个SL4A站点安装Python for Android。会的
下载r4版本的apk。
启动SL4A应用程序并检查HelloWorld python脚本
跑步。它将确保您的安装是好的。
现在是安装Cherrypy库模块的时候了。可以找到它
在http://www.cherrypy.org/wiki/CherryPyDownload。我拿了3.2.0
版本。下载Python2.6的egg。
现在我们需要将egg文件传输到Android模拟器。使用adb
push命令将egg文件传输到/mnt/sdcard/Download。
从模拟器启动Python for Android并点击Import modules。
它将列出新上传的egg文件。选择并安装。
现在我们可以编写一个Cherrypy脚本作为CGI运行。下面是
HelloWorld示例取自Cherrypy教程(稍作修改)# Import CherryPy global namespace
import cherrypy
class HelloWorld:
""" Sample request handler class. """
def index(self, **params):
# CherryPy will call this method for the root URI ("/") and send
# its return value to the client.
for key in params:
print key, '=', params[key]
return "Hello world!"
# Expose the index method through the web. CherryPy will never
# publish methods that don't have the exposed attribute set to True.
index.exposed = True# CherryPy always starts with app.root when trying to map request URIs
# to objects, so we need to mount a request handler root. A request
# to '/' will be mapped to HelloWorld().index().
cherrypy.config.update({'server.socket_host': '127.0.0.1'})
cherrypy.config.update({'server.socket_port': 8080})
cherrypy.quickstart(HelloWorld(), '/')
8>;脚本需要传输到emulator中的/mnt/sdcard/sl4a/scripts目录。然后可以启动SL4A并点击脚本运行。如果在SL4A控制台中运行,则可以看到调试输出。
9>;在Android浏览器中,检查URL http://localhost:8080/。它会说“你好,世界”。
这是一个web服务器集,我们可以放置python脚本来访问Android手机数据和其他可以向外部客户端公开的东西。
其他信息:
我的建议:
我建议在raspberry pi中使用任何轻量级web服务器(比如node.js)
参考号: