从os.system开始时,chrome无法打开完整的URL(py) [英] chromium not opening full URL when started with os.system (py)

查看:397
本文介绍了从os.system开始时,chrome无法打开完整的URL(py)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用os.system在Chrome中打开一个URL,将GET参数传递给php页面。但是,铬似乎不接受或识别不止一个参数。

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)

正在打印的字符串:
Chrome浏览器localhost / index.php?temp = 15& id = 10

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

正在打开URL:
http://localhost/index.php?temp = 15

使用引号将URL包裹即可解决此问题。

Wrapping the URL in quotes solved the issue.

推荐答案

您正在传递命令到子壳。 &符号在Unix shell中有特殊含义。它将放在前面的命令中背景

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.

如果要从命令行运行此命令,则完全忽略Python:

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.

要解决此问题,您需要使用&符号;最好的方法可能只是包装要用引号引起来的整个URL:

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()))

这篇关于从os.system开始时,chrome无法打开完整的URL(py)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆