Python直接从命令行读参数
来源:互联网 发布:json压缩工具 编辑:程序博客网 时间:2024/06/05 16:46
由来
由于缺乏某python程序直接外部的API调用的支持,于是打算直接使用该python工具内部的api,通过几步设置好环境以后,该API可以调用并且成功。但是,发现调用应用的目标是错误的,在服务器端调用该api不能达到处理的目的,这个api应该在目标板端执行,也就是通过板子命令行执行。
解决之道
1. 板子缺乏相应的python支持,但好在是Ubuntu的系统,安装该python工具需要的包后,将该工具包源码下载,并安装到板子系统中,取得了这个包的API支持。
2. Python的-c参数可以直接在命令行中执行一个python命令
$ python -c 'import foo; print foo.hello()'
3. 注意服务器送入板子端的指令应该是raw指令,即不会自动进行变量代换比如"必须写为\" cmd = ("python -c 'import linaro_image_tools.media_create.boards"
" as lmc_boards; board_type = \"%s\"; rootfs_uuid = \"\";"
" boot_partition = \"/dev/disk/by-label/testboot\";"
" boot_disk = \"/mnt/boot\"; chroot_dir = \"/mnt/root\";"
" boot_device_or_file = \"/dev/null\"; is_live = False;"
" is_lowmem = False; consoles = \"\";"
" board = lmc_boards.board_configs[board_type]();"
" board.set_metadata([\"/mnt/root/tmp/%s\"]);"
" board.populate_boot(chroot_dir, rootfs_uuid, boot_partition, "
"boot_disk, boot_device_or_file, is_live, is_lowmem, consoles)'"
% (board_type, hwpack_name))
参考文档
http://stackoverflow.com/questions/3987041/python-run-function-from-the-command-line
问题由来代码
延伸阅读
该插件要求在每一个module下都加上一段语句,该module中的函数才能被调用,见Usage
- """
- PythonCall.py
- PythonCall is a shortcut that allows a CLI command with arguments to call
- any function within a Python module with only two lines of plumbing code.
- Usage
- =====
- * Add code below to bottom of Python module.
- if __name__ == "__main__":
- import sys, PythonCall
- PythonCall.PythonCall(sys.argv).execute()
- * From command line or desktop shortcut, build a command of form:
- <pythonpath> <module> <function> <arg1 arg2 ...>
- Example: C:\Python25\python.exe C:\Dev\PyUtils\src\TextUtils.py xclip wrap 64
- Notes
- =====
- * Called functions must expect args to be strings and do their own conversions.
- * No argument checking or error-checking.
- * In case of exception, PythonCall sends message to stderr.
- * I often use this with text I pass in via the clipboard, which requires
- code to read and write the clipboard (not included here).
- Tested with Windows; should work on other platforms.
- Jack Trainor 2008
- """
- import os, os.path
- import sys
- import types
- class PythonCall(object):
- def __init__(self, sysArgs):
- try:
- self.function = None
- self.args = []
- self.modulePath = sysArgs[0]
- self.moduleDir, tail = os.path.split(self.modulePath)
- self.moduleName, ext = os.path.splitext(tail)
- __import__(self.moduleName)
- self.module = sys.modules[self.moduleName]
- if len(sysArgs) > 1:
- self.functionName = sysArgs[1]
- self.function = self.module.__dict__[self.functionName]
- self.args = sysArgs[2:]
- except Exception, e:
- sys.stderr.write("%s %s\n" % ("PythonCall#__init__", e))
- def execute(self):
- try:
- if self.function:
- self.function(*self.args)
- except Exception, e:
- sys.stderr.write("%s %s\n" % ("PythonCall#execute", e))
- #####################################################
- # Test Examples - REMOVE
- #
- # Normally the function calls are in a module other than PythonCall.
- # These are only examples.
- #
- # Example 1: C:\Python25\python.exe C:\Dev\PyUtils\src\PythonCall.py double 14
- # Example 2: C:\Python25\python.exe C:\Dev\PyUtils\src\PythonCall.py capitalize "Four score and seven years ago..."
- #####################################################
- def double(x):
- x = int(x)
- print 2 * x
- def capitalize(s):
- print s.upper()
- if __name__ == "__main__":
- import sys, PythonCall
- PythonCall.PythonCall(sys.argv).execute()
- #####################################################
- # End - REMOVE
- #####################################################
- Python直接从命令行读参数
- 从命令行输入参数
- python 命令行参数
- python处理命令行参数
- python 命令行参数
- Python命令行参数选择
- python命令行参数
- python命令行参数
- python 命令行参数
- python 处理命令行参数
- Python命令行参数选择
- Python获取命令行参数
- Python获得命令行参数
- python处理命令行参数
- python处理命令行参数
- python 处理命令行参数
- python命令行参数处理
- python 获取命令行参数
- 常用dos命令 及 语法
- 二进制、八进制、十进制、十六进制的转换
- centos下安装jdk
- 常见的搜索引擎的原则
- dom解析XML文件操作
- Python直接从命令行读参数
- java 学习笔记--起步1
- csdn k歌之王(音视频解码)
- rman实验之归档模式无备份,正常关机丢失数据文件的恢复
- Unix系统的SHELL程序如何将字符串分割成数组
- GDB十分钟教程
- aop简单注解实现例程
- 最新Linux(CentOS5.5)搭建GFS系统--iSCSI+GFS实现网络存储
- 程序运行时提示内存----出错