2

I am doing some wget calls within a python script, where I issue some PUT method to send some commands, but when python is parsing the wget command which I would like to send out, it reports an KeyError on a variable within the wget call.

The command that I create is

wget = "wget --http-user={user} --http-password={password} --header='Accept:application/json' --header='Content-Type:application/json' --method=PUT --body-data='{'mode':'{bodyD}'}' -O- http://{IPaddress}/api/{v}/{cm}" .format(user=usr,password=pswd,IPaddress=node,v=version,bodyD=bodyData,cm=Command)

And the KeyError is "KeyError:'mode'", when sending out that command. What is the error coming from? Do I have to scape the word "mode" because it is reserved?

Thanks in advance,

Regards,

1

3 Answers 3

1

Try it with double {{ }} braces like so:

wget = "wget --http-user={user} --http-password={password} --header='Accept:application/json' --header='Content-Type:application/json' --method=PUT --body-data='{{'mode':'{bodyD}'}}' -O- http://{IPaddress}/api/{v}/{cm}" .format(user=usr,password=pswd,IPaddress=node,v=version,bodyD=bodyData,cm=Command)
Sign up to request clarification or add additional context in comments.

Comments

1

Use double {{ }} as suggested in https://stackoverflow.com/a/5466478/968442,

Also BodyData usually would be JSON which might required double quotes.

usr = "test"
pswd = "test"
node = "test"
version = "test"
bodyData = "test"
Command = "test"
wget = """wget --http-user={user} --http-password={password} --header='Accept:application/json' --header='Content-Type:application/json' --method=PUT --body-data='{{"mode":"{bodyD}"}}' -O- http://{IPaddress}/api/{v}/{cm}""".format(user=usr,password=pswd,IPaddress=node,v=version,bodyD=bodyData,cm=Command)

print wget

Logs:

> python test.py
wget --http-user=test --http-password=test --header='Accept:application/json' --header='Content-Type:application/json' --method=PUT --body-data='{"mode":"test"}' -O- http://test/api/test/test

Comments

0

I have tried to use the reply from itsneo, but it didn't work. However, that gave me a hint and doubling the curly braces it did.

So this command does work

wget = "wget --http-user={user} --http-password={password} --header='Accept:application/json' --header='Content-Type:application/json' --method=PUT --body-data='{{mode:{bodyD}}}' -O- http://{IPaddress}/api/{v}/{cm}" .format(user=usr,password=pswd,IPaddress=node,v=version,bodyD=bodyData,cm=Command)

1 Comment

As I mentioned in my answer, Json would require you to strictly use double quotes. So you might have to escape now or go for Python's triple quote style.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.