简体   繁体   English

将python脚本的参数作为参数传递到它的函数中

[英]Passing python script's argument into its function as argument

I am trying to write a python script which needs to take input arguments, to be used inside the script when calling a function.我正在尝试编写一个需要输入参数的 python 脚本,以便在调用函数时在脚本内部使用。

Following is working inside python shell:以下是在 python shell 中工作:

>>> result = client.call('SoftLayer_Hardware', 'getAllObjects', filter={'id': {'operation': '1442567'}}, limit=300, mask='mask[id, hostname, accountId, primaryIpAddress, softwareComponents[passwords], upstreamHardware[id, hostname], uplinkNetworkComponents[id, name, port, uplink[uplinkComponent[id, hardwareId, name, port]]]]')
>>> 

This way is also working properly:这种方式也正常工作:

>>> var_f = {'id': {'operation': '1442567'}}
>>> var_m = "mask[id, hostname, domain, accountId, datacenter, networkVlans, networkManagementIpAddress, primaryBackendIpAddress, primaryIpAddress, softwareComponents[modifyDate, passwords[username, password], softwareLicense[softwareDescriptionId, softwareDescription[longDescription]]], uplinkNetworkComponents[id, name, port, primaryIpAddress, networkVlanId, macAddress, speed, status, uplink[id, uplinkComponent[id, hardwareId, name, port, duplexModeId, maxSpeed, speed, status, networkPortChannelId, networkVlanId]]], upstreamHardware[id, hostname]]"
>>> result = client.call('SoftLayer_Hardware', 'getAllObjects', filter=var_f, limit=300, mask=var_m)
>>>

However when I write similar code in script file:但是,当我在脚本文件中编写类似的代码时:

#!/usr/bin/python

import sys
import softlayer_api_common as ims
client = ims.connect_to_api(neteng_user=True)

filterinput = sys.argv[1]
maskinput = sys.argv[2]

print 'First argument'
print filterinput
print 'Second argument'
print maskinput

result = client.call('SoftLayer_Hardware', 'getAllObjects', filter=filterinput, limit=300, mask=maskinput)

sys.exit()

Then calling it with two argument as follows, it is failing:然后用两个参数调用它,如下所示,它失败了:

$ ./imsquery "{'id': {'operation': '1442567'}}" "mask[id, hostname, domain, accountId, datacenter, networkVlans, networkManagementIpAddress, primaryBackendIpAddress, primaryIpAddress, softwareComponents[modifyDate, passwords[username, password], softwareLicense[softwareDescriptionId, softwareDescription[longDescription]]], uplinkNetworkComponents[id, name, port, primaryIpAddress, networkVlanId, macAddress, speed, status, uplink[id, uplinkComponent[id, hardwareId, name, port, duplexModeId, maxSpeed, speed, status, networkPortChannelId, networkVlanId]]], upstreamHardware[id, hostname]]"
First argument
{'id': {'operation': '1442567'}}
Second argument
mask[id, hostname, domain, accountId, datacenter, networkVlans, networkManagementIpAddress, primaryBackendIpAddress, primaryIpAddress, softwareComponents[modifyDate, passwords[username, password], softwareLicense[softwareDescriptionId, softwareDescription[longDescription]]], uplinkNetworkComponents[id, name, port, primaryIpAddress, networkVlanId, macAddress, speed, status, uplink[id, uplinkComponent[id, hardwareId, name, port, duplexModeId, maxSpeed, speed, status, networkPortChannelId, networkVlanId]]], upstreamHardware[id, hostname]]

Traceback (most recent call last):
  File "./imsquery", line 15, in <module>
    result = client.call('SoftLayer_Hardware', 'getAllObjects', filter=filterinput, limit=300, mask=maskinput)
  File "/usr/lib/python2.7/site-packages/SoftLayer/API.py", line 265, in call
    return self.transport(request)
  File "/usr/lib/python2.7/site-packages/SoftLayer/transports.py", line 247, in __call__
    raise exceptions.TransportError(ex.response.status_code, str(ex))
SoftLayer.exceptions.TransportError: TransportError(500): 500 Server Error: Internal Server Error for url: https://internal.applb.dal01.softlayer.local/v3/internal/xmlrpc/SoftLayer_Hardware
$

Looks the script's input arguments are being taken into the variables, however having problem with using those variable's contents when calling the function, not working with this syntax.看起来脚本的输入参数被带入变量中,但是在调用函数时使用这些变量的内容存在问题,而不是使用此语法。 Any suggestions?有什么建议?

It fails because of argument filterinput .由于参数filterinput失败。 It is expected to be dict type as input to internal function.预计是dict类型作为内部函数的输入。 But being passing from command line it doesn't convert to dict automatically.但是从命令行传递它不会自动转换为dict Here, try to print your filterinput type:在这里,尝试打印您的filterinput输入类型:

Print Output:打印输出:

...
First argument
{'id': {'operation': '1442567'}}
<class 'str'>
Second argument
...

You have to convert it from str to dict manually using json.loads() , for example例如,您必须使用json.loads()手动将其从str转换为dict

Code:代码:

#!/usr/bin/python

import sys
import json
import softlayer_api_common as ims
client = ims.connect_to_api(neteng_user=True)

filterinput = json.loads(sys.argv[1])
maskinput = sys.argv[2]
...

BUT: Don't forget use json standard for your argument, so instead of "{'id': {'operation': '1442567'}}" you should pass '{"id": {"operation": 1442567}}' :但是:不要忘记为您的参数使用 json 标准,因此您应该传递'{"id": {"operation": 1442567}}'而不是"{'id': {'operation': '1442567'}}" '{"id": {"operation": 1442567}}' :

$ ./imsquery '{"id": {"operation": 1442567}}' "mask[id, hostname, domain, accountId, datacenter, networkVlans, networkManagementIpAddress, primaryBackendIpAddress, primaryIpAddress, softwareComponents[modifyDate, passwords[username, password], softwareLicense[softwareDescriptionId, softwareDescription[longDescription]]], uplinkNetworkComponents[id, name, port, primaryIpAddress, networkVlanId, macAddress, speed, status, uplink[id, uplinkComponent[id, hardwareId, name, port, duplexModeId, maxSpeed, speed, status, networkPortChannelId, networkVlanId]]], upstreamHardware[id, hostname]]"

Print Output:打印输出:

First argument
{'id': {'operation': 1442567}}
<class 'dict'>
Second argument
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM