Created
March 27, 2019 07:56
-
-
Save huhuhang/ddd08debd4d7ba33ff7a4981137a9591 to your computer and use it in GitHub Desktop.
Flask run in Jupyter Notebook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Overwriting run.py\n" | |
] | |
} | |
], | |
"source": [ | |
"%%writefile run.py\n", | |
"from flask import Flask\n", | |
"\n", | |
"app = Flask(__name__)\n", | |
"\n", | |
"@app.route(\"/\")\n", | |
"def demo():\n", | |
" return \"Hello, word!\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<subprocess.Popen at 0x7fffec19fb70>" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import subprocess as sp\n", | |
"\n", | |
"# 启动子进程执行 Flask app\n", | |
"server = sp.Popen(\"FLASK_APP=run.py flask run\", shell=True)\n", | |
"server" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"b'Hello, word!'" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import requests\n", | |
"\n", | |
"requests.get(url=\"http://127.0.0.1:5000\").content" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"server.terminate() # 结束子进程,关闭端口占用" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
get 时总是报错怎么办?
ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001F2DAE51910>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))
试过搜到的方案:
proxies = { "http": None, "https": None}
os.environ['NO_PROXY'] = 'http://127.0.0.1:5000'
session = requests.Session()
session.trust_env = False
session.get(url="http://127.0.0.1:5000", proxies=proxies).content
还是未解决报错