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,

ndarkness
  • 1,011
  • 2
  • 16
  • 36
  • Just a food for thought: consider sending requests using requests library: http://docs.python-requests.org/en/latest/ – Nebril Dec 10 '15 at 09:40

3 Answers3

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
Community
  • 1
  • 1
nehem
  • 12,775
  • 6
  • 58
  • 84
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)
mirosval
  • 6,671
  • 3
  • 32
  • 46
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)
ndarkness
  • 1,011
  • 2
  • 16
  • 36
  • 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. – nehem Dec 10 '15 at 09:51