使用os.system(py)启动时,chrome无法打开完整的URL

I tried to open a url in chromium with os.system, passing GET arguments to the php page. However it seems that chromium doesn't accept or recognize more than one argument.

url = "chromium-browser localhost/index.php?temp=" + str(int(math.floor(get_temperature()))) + "&id=" + get_id()
print(url)
os.system(url)

String being printed: chromium-browser localhost/index.php?temp=15&id=10

URL being opened: http://localhost/index.php?temp=15


Solved

Wrapping the URL in quotes solved the issue.

You're passing a command to a subshell. The ampersand has a special meaning to a Unix shell; it puts the preceding command in the background.

Ignoring Python completely, if you were to run this from the command line:

chromium-browser localhost/index.php?temp=15&id=10

...you'd find that it would execute the command:

chromium-browser localhost/index.php?temp=15

...in the background, and then attempt to execute the command:

id=10

in the foreground. That last bit would likely fail, as it's not a valid command, but the first command would succeed.

To solve the problem, you need to escape the ampersand; the best way to do this is probably just to wrap the whole URL you're passing in quotes:

chromium-browser "localhost/index.php?temp=15&id=10"

So perhaps something like this would be appropriate:

command_line='chromium-browser "http://localhost/index.php?temp={0}&id={1}"'
os.system(command_line.format(math.floor(get_temperature()), get_id()))