【问题标题】:Opening the browser from Python running in Google Cloud Shell从在 Google Cloud Shell 中运行的 Python 打开浏览器
【发布时间】:2019-01-16 14:14:33
【问题描述】:

我正在 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 或其他库都没关系。

【问题讨论】:

    标签: python-3.x google-cloud-platform google-cloud-shell


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      • 2022-12-28
      • 2017-09-22
      • 1970-01-01
      相关资源
      最近更新 更多