chromium not opening full URL when started with os.system (py)

孤人 提交于 2019-12-12 04:54:27

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/45923331/chromium-not-opening-full-url-when-started-with-os-system-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!