pythonbashshelldocopt

Passing Python list in single quote


I have a piece of code like below which is used to send a list of IP addresses to an API call.

body = {'cID': id, 'dbType': params['db-type'].upper(), 'cidrList': eval(params['--cidr-list'])}
print(json.dumps(body))
conn.request("POST", "/Link/to/API", body=json.dumps(body), headers=header)
check_resp(200)
logger.info("Rules changed successfully")

However, when I call this code using the below params, it fails.

--cidr-list ['10.20.0.0/32','10.30.0.0/32']

It works when I use the below.

--cidr-list [\"10.20.0.0/32\",\"10.30.0.0/32\"]

So basically when I use \" to wrap each item of the list, it is parsed as single quotes. How do I change the code so that it accepts input 1? I'm new to Python and I would appreciate if you could explain the logic behind it as well. Thanks in advance.


Solution

  • Don't make knowledge of Python a requirement to use your program.

    body = {
        'cID': id,
        'dbType': params['db-type'].upper(),
        'cidrList': params['--cidr-list'].split(',')
    }
    print(json.dumps(body))
    conn.request("POST", "/Link/to/API", json=body, headers=header)
    check_resp(200)
    logger.info("Rules changed successfully")
    

    Then invoke the script with

    script.py ... --cidr-list 10.20.0.0/32,10.30.0.0/32
    

    All arguments are already strings; you don't need to force Python string-literal syntax on the user, and a comma-separated string is sufficient to process into a list of CIDR addresses without forcing Python list syntax on the user as well.