将curl命令从google-colab转换为python脚本 [英] Convert curl command from google-colab to python script

查看:226
本文介绍了将curl命令从google-colab转换为python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Google colab代码:

I have the following google colab code:

代码:

!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip

!unzip ngrok-stable-linux-amd64.zip

LOG_DIR = './log'
get_ipython().system_raw(
    'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'
    .format(LOG_DIR)
)

get_ipython().system_raw('./ngrok http 6006 &')    
! curl -s http://localhost:4040/api/tunnels | python3 -c \
    "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"

输出:
https:/ /6a112ff8.ngrok.io

我的问题是如何将curl pipe python命令(最后3行)转换为python脚本?目前,它正在google colab中执行。

My question is How do I convert the curl pipe python command (last 3 lines) into a python script ? Currently it is being executed in google colab.

我尝试使用以下代码接近解决方案:

I have tried to get close to solution using this code :

import sys, json
import requests
from IPython import get_ipython


LOG_DIR = './log'

get_ipython().system_raw(
    'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'
    .format(LOG_DIR)
)

response = requests.get('http://localhost:4040/api/tunnels')
# result=json.load(response)
print(json.load(response)['tunnels'][0]['public_url'])

但是我得到一个错误:

AttributeError: 'NoneType' object has no attribute 'system_raw'


推荐答案

感谢@ v25注释,我能够使用python运行它。这就是我的理解。

Thanks to @v25 comment I am able to run it using python. Here's what I understood.

警告:在Macos上,ngrok存在某种权限问题,因此我很高兴地在MacOS上运行了demo.py脚本。我在Ubuntu 16上运行了它

Warning: On Macos there is some kind of permission issue with ngrok, hence I coudnt get the fist demo.py script running on macos. I got it running on Ubuntu 16

我下载了这些文件并将它们放在存在demo.py和demo2.py的文件夹中

I downloaded these files and placed them in a folder where demo.py and demo2.py are present

wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip

unzip ngrok-stable-linux-amd64.zip

我制作了2个脚本:

demo.py(正常情况下)

demo.py (nominal case)

import os
os.system('./ngrok http 8000 &')

用tensorboard将os.system替换为:

for tensorboard replace os.system with:

os.system('tensorboard --logdir {} --host 0.0.0.0 --port 8001 &'.format(LOG_DIR))

demo.py将在一个终端窗口中运行。

demo.py will run in one terminal window.

demo2.py

import os
import requests

try:
    r = requests.get('http://localhost:4040/api/tunnels')
    d = r.json() 
    public_url = d['tunnels'][0]['public_url']
    print(public_url)
except Exception as e:
    print ('Failed: ', e)

demo2.py将在另一个终端窗口中运行。

demo2.py will run in another terminal window.

demo2.py将生成一个我可以使用的网址。请参阅v25的注释,详细了解为什么需要2个独立的终端窗口。我正在添加此解决方案,以供将来参考。

demo2.py will produce a url which I can use. Please refer to v25's comment to see in details why 2 seperate terminal windows are needed. I am adding this solution for my future reference.

这篇关于将curl命令从google-colab转换为python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆