gpt4 book ai didi

python-3.x - 从在 Google Cloud Shell 中运行的 Python 打开浏览器

转载 作者:行者123 更新时间:2023-12-05 05:15:16 28 4
gpt4 key购买 nike

我正在 Python 3.6 上开发一个小脚本,我很可能会在 Google Cloud Shell 中使用它。在上述脚本中,我想进行一些 API 调用,然后使用这些调用的结果 URL 打开 Web 浏览器。以下代码适用于我测试过的其他环境,但不适用于 Cloud Shell:

import webbrowser as wb
# different calls and process here, not relevant to the issue
wb.open('URL_HERE')
#This just echoes the URL.

有没有办法让 Python“告诉”Cloud Shell 从它运行的地方使用浏览器?即:如果我使用 Chrome 打开 Cloud Shell,有没有办法在 Chrome 中打开链接?无论是使用 webbrowser 还是其他库都没有关系。

最佳答案

Cloud Shell 只是一个“窗口”,显示来自远程和 temporary Compute Engine virtual machine instance 的命令行.这意味着当您运行脚本时,您实际上是在远程 VM 中运行它(而不是在 Chrome 中),并且该特定 VM 本身没有浏览器。

例如,当您尝试在 Cloud Shell 中运行应用程序时(here 您可以使用“mvn appengine:run”找到一个快速示例),应用程序运行后,您将在 Cloud Shell 中看到一条消息,类似于:

[信息] GCLOUD:信息:模块实例默认运行在 http://localhost:8080/

如果您点击 http://localhost:8080/ ,您实际上将被重定向到为 Cloud Shell VM 实例分配的临时地址(类似于 8080-dot-VM-ID-devshell.appspot.com)。

总而言之,您无法命令 Cloud Shell 打开带有远程 VM 命令行中显示的特定 URL 的浏览器。另外,你应该考虑有 outgoing connections limitations Cloud Shell 适用于 interactive use仅。

关于python-3.x - 从在 Google Cloud Shell 中运行的 Python 打开浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51761503/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com