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

时间:2017-08-28 16:16:47

标签: php python chromium

我尝试用os.system打开一个chrome的url,将GET参数传递给php页面。然而,似乎铬并不能接受或认识到不止一个论点。

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

正在打印的字符串: chromium-browser localhost / index.php?temp = 15& id = 10

正在打开的网址: http://localhost/index.php?temp=15

解决

用引号括起网址解决了这个问题。

1 个答案:

答案 0 :(得分:0)

您将命令传递给子shell。 &符号对Unix shell有特殊意义;它puts the preceding command in the background

完全忽略Python,如果你要从命令行运行它:

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

...你发现它会执行命令:

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

...在后台,然后尝试执行命令:

id=10

在前台。最后一点可能会失败,因为它不是有效命令,但第一个命令会成功。

要解决这个问题,你需要逃避&符号;这样做的最好方法可能只是将您传入引号的整个URL包裹起来:

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

所以也许这样的事情是合适的:

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