通过ProcessBuilder将命令行参数传递给Python

时间:2016-07-29 19:29:58

标签: python

当我使用python InterWebApp.py users.xml从终端运行我的脚本时,这是我得到的错误:

Traceback (most recent call last):
File "InterWebApp.py", line 43, in <module>
app.run()
File "/usr/lib/python2.6/site-packages/web/application.py", line 313, in run
return wsgi.runwsgi(self.wsgifunc(*middleware))
File "/usr/lib/python2.6/site-packages/web/wsgi.py", line 55, in runwsgi
server_addr = validip(listget(sys.argv, 1, ''))
File "/usr/lib/python2.6/site-packages/web/net.py", line 120, in validip
raise ValueError, ':'.join(ip) + ' is not a valid IP address/port'
ValueError: users.xml is not a valid IP address/port  

此外,该脚本不会与Java中的参数一起运行:

ProcessBuilder pb = new ProcessBuilder("python", script.getAbsolutePath(), xml.getAbsolutePath());
Process p = pb.start();

这是我的剧本:

#!/usr/bin/env python
import web
import sys
import xml.etree.ElementTree as ET

file = sys.argv[1]

urls = ('/get_user/(.*)', 'get_user',
    '/users', 'list_users')

app = web.application(urls, globals())

def read_xml(xml_file):
    with open(xml_file, 'r') as f:
        tree = ET.parse(f)
    return tree.getroot()

root = read_xml(file)

class list_users:
    def GET(self):
        output = 'Users:\n'
        for child in root:
            print 'child', child.tag, child.attrib
            output += (str(child.attrib['user']) +
            ': ' + str(child.attrib['name']) + ', ' + str(child.attrib['email'] + '\n'))
        return output

class get_user:
    def GET(self, name):
        for child in root:
            if child.attrib['name'] == name:
                output += str(child.attrib['name']) + ", " + str(child.attrib['email'])
            return output
        output = "The user (" + name + ") couldn't be found!"
        return output

if __name__ == "__main__":
    app.run()

为执行脚本提供命令行参数有什么错误?

0 个答案:

没有答案
相关问题