【问题标题】:chromium not opening full URL when started with os.system (py)使用 os.system (py) 启动时,铬没有打开完整的 URL
【发布时间】:2017-08-28 16:16:46
【问题描述】:

我尝试使用 os.system 在 chromium 中打开一个 url,将 GET 参数传递给 php 页面。然而,铬似乎不接受或承认不止一个论点。

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

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

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


已解决

将 URL 用引号括起来解决了这个问题。

【问题讨论】:

  • 如果您在操作系统的命令行中键入chromium-browser localhost/index.php?temp=15&id=10,会发生什么? (提示:您可能想尝试将参数用引号括起来)
  • 谢谢,解决问题
  • 我推荐使用{}.format 样式而不是使用+ 来连接:url = "chromium-browser localhost/index.php?temp={}&id={}".format(math.floor(get_temperature()), get_id())——它会将所有类型强制转换为字符串,因此您不必包装参数.

标签: php python chromium


【解决方案1】:

您正在将命令传递给子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()))

【讨论】:

    猜你喜欢
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多