简体   繁体   English

从运行在Google Cloud Shell中的Python打开浏览器

[英]Opening the browser from Python running in Google Cloud Shell

I'm developing a small script on Python 3.6, and I'll most likely use it from Google Cloud Shell. 我正在Python 3.6上开发一个小脚本,很可能会在Google Cloud Shell中使用它。 In said script, I want to do some API calls, and then, open the web browser with an URL, result of those calls. 在上述脚本中,我想进行一些API调用,然后使用这些调用的结果打开带有URL的Web浏览器。 The following code works in other environments that I have tested on, but not in Cloud Shell: 以下代码可在我测试过的其他环境中使用,但不能在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.

Is there anyway to make Python 'tell' Cloud Shell to use the browser from where it's running? 无论如何,是否要使Python“告诉” Cloud Shell在运行它的地方使用浏览器? ie: if I'm using Chrome to open Cloud Shell, is there any way to open the link in Chrome? 即:如果我使用Chrome打开Cloud Shell,是否可以在Chrome中打开链接? It doesn't matter if it's with webbrowser or other library. 是否与网络浏览器或其他库无关。

Cloud Shell is just a “window” displaying a command line from a remote and temporary Compute Engine virtual machine instance . Cloud Shell只是一个“窗口”,显示来自远程临时Compute Engine虚拟机实例的命令行。 Meaning that when you run the script, you are actually running it in the remote VM (not in Chrome), and that specific VM does not have a browser by itself. 这意味着在运行脚本时,实际上是在远程VM(而不是Chrome)中运行脚本,并且特定VM本身没有浏览器。

For example, when you try to run an app in Cloud Shell ( here you can find a quick example using "mvn appengine:run"), once the application is running, you will see a message in Cloud Shell, something like: 例如,当您尝试在Cloud Shell中运行应用程序时( 在这里可以找到一个使用“ mvn appengine:run”的快速示例),一旦该应用程序运行,您将在Cloud Shell中看到一条消息,类似于:

[INFO] GCLOUD: INFO: Module instance default is running at http://localhost:8080/ [INFO] GCLOUD:INFO:模块实例默认运行在http:// localhost:8080 /

If you click on http://localhost:8080/ , you will actually be redirected to the temporary address assigned for the Cloud Shell VM instance (something like 8080-dot-VM-ID-devshell.appspot.com). 如果单击http:// localhost:8080 / ,则实际上会将您重定向到为Cloud Shell VM实例分配的临时地址(类似8080-dot-VM-ID-devshell.appspot.com)。

In summary, you can't command Cloud Shell to open the browser with a specific URL shown in the remote VM command line. 总而言之,您无法命令Cloud Shell使用远程VM命令行中显示的特定URL打开浏览器。 Also, you should consider that there are outgoing connections limitations and Cloud Shell is intended for interactive use only. 此外,您应该考虑到传出连接的限制,并且Cloud Shell仅用于交互式使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM