首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法在python中调用ngrok

无法在python中调用ngrok
EN

Stack Overflow用户
提问于 2020-10-07 11:58:16
回答 1查看 92关注 0票数 1

我在谷歌的协作室里运行flask。

代码语言:javascript
运行
复制
import os

html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>

Hello
<form action="{{ url_for('submit') }}" method="post">
    <textarea name="text"></textarea>
    <input type="submit">
</form>

</body>
</html>
"""

with open("pegasus/bin/evaluate.py"):
  if not os.path.isdir( "templates" ):
      os.makedirs( "templates" )
  with open("templates/index.html", mode='w') as f:
      f.write(html)

  from flask import Flask, render_template, request

  app = Flask(__name__)

  @app.route("/")
  def index():
      return render_template( "index.html" )

  @app.route('/submit', methods=['POST'])
  def submit():
      hello_eval()
      # request.formがユーザーの送信した文字列を保持するようになっている。
      return 'You entered: {}'.format(request.form['text'])

  if __name__ == '__main__':
      app.run(port=6006)

这是我的flask代码,我想在浏览器中访问index.html。所以我粘贴了一个代码来获取ngrok的url。

代码语言:javascript
运行
复制
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'])"

但是代码抛出了一个错误。

代码语言:javascript
运行
复制
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.6/json/__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我记得我在大约两周前正确地运行了它。我能为此做些什么呢?

帮帮我!

EN

回答 1

Stack Overflow用户

发布于 2020-10-08 13:09:05

我通过像这样修改python代码解决了这个问题。

代码语言:javascript
运行
复制
import os
from flask_ngrok import run_with_ngrok
from flask import Flask, render_template, request

html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>

Hello
<form action="{{ url_for('submit') }}" method="post">
    <textarea name="text"></textarea>
    <input type="submit">
</form>

</body>
</html>
"""

app = Flask(__name__)
run_with_ngrok(app)

with open("pegasus/bin/evaluate.py"):
  if not os.path.isdir( "templates" ):
      os.makedirs( "templates" )
  with open("templates/index.html", mode='w') as f:
      f.write(html)


  @app.route("/")
  def index():
      return render_template( "index.html" )

  @app.route('/submit', methods=['POST'])
  def submit():
      hello_eval()
      # request.formがユーザーの送信した文字列を保持するようになっている。
      return "'You entered:' {}".format(request.form['text'])

  if __name__ == '__main__':
      app.run()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64236981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档