'how to call curl in python with variables defined?

I have a python script which should execute curl command. I used os.system. command is:

os.system("curl -d 'protection=$protection&Code=$Code' -X POST https://xyz.somecompany.com/web/services/final.php")

and I get and error:

Code missingCode missingWorker with sessionID:xyz

I gues this is a problem with variable which is defined in python script but it can not be called in curl command? or im wrong?

variables are defined in python script as follows:

Code = item.decode("utf-8")
protection = (int(Code) - int(year)) / 2


Solution 1:[1]

You can do it like this:

cmd = "curl -d 'protection={protection}&Code={code}' -X POST https://xyz.somecompany.com/web/services/final.php"
os.system(cmd.format(protection=protection, code=Code))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 var211