Convert curl command from google-colab to python script

ぃ、小莉子 提交于 2020-01-15 03:41:33

问题


I have the following google colab code:

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'])"

output: https://6a112ff8.ngrok.io

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'])

However I get an error:

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

回答1:


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

To do this without curl in pure python, you could use the requests library, and the json method of the response object (r in this example) which returns a dictionary:

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

# Do something with `public_url`



回答2:


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

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

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

I made 2 scripts:

demo.py (nominal case)

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

for tensorboard replace os.system with:

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

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 will run in another terminal window.

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.



来源:https://stackoverflow.com/questions/59336142/convert-curl-command-from-google-colab-to-python-script

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