いつも大変お世話になっております。
表記の件につきまして、cloud9にてFlaskを利用して簡単なアプリケーションを書き、起動させたのですがアクセス出来なくて困っております。
書いたコードは以下の通りです。
python
1#!/bin/env python 2# coding: utf-8 3 4from flask import Flask 5app = Flask(__name__) 6 7@app.route("/") 8def hello(): 9 return "Hello World!" 10 11if __name__ == "__main__": 12 app.run(host='0.0.0.0', port=8080)
アプリの実行時には
- Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
の記載があり、サーバーは起動しているように見受けられます。
pythonのversionは[バッドノウハウ]webプログラミング初学者がAWS Cloud9でPython3を使うを参考にし、python3.6.2となっております。
解決方法をご教示願います。
**追記:**ご指摘ありがとうございます。
アプリ実行時に表示されている「http://0.0.0.0:8080/」をgoogle chromeでアクセスした結果、
ページを表示できません
サーバーに接続できません。
サーバーが存在しないか、接続拒否している可能性があります。
と、表示されてしまいます。
また、別の話になってしまうのですが、旧cloud9(AWS版では無いもの)で別のFlaskアプリ(以下に記載)を起動させ、同様にアクセスすると
No application seems to be running here!
Cloud9 can't get you to your requested workspace. Here are some suggestions on how to figure out what's going on:Check that the workspace name (stockpile) and username (nokonoko123) are typed correctly.
Check that the server is successfully running on Cloud9:
If the server hit an error, the output window will have a message telling you what it is
If you're in the middle of debugging code, your server might be paused right now
The server might be running on a different port; make sure it's on port $PORT with $IP as the IP address
と表示されてアクセスできませんでした。
アプリのコードは以下のようになります。
python
1#!/usr/bin/env python 2# coding: utf-8 3 4# Flask などの必要なライブラリをインポートする 5from flask import Flask, render_template, request, redirect, url_for 6import random 7 8# 自身の名称を app という名前でインスタンス化する 9app = Flask(__name__) 10 11# メッセージをランダムに表示するメソッド 12def picked_up(): 13 messages = [ 14 "こんにちは、あなたの名前を入力してください", 15 "やあ!お名前は何ですか?", 16 "あなたの名前を教えてね" 17 ] 18 # random.choice で配列からランダムに取り出し 19 return random.choice(messages) 20 21# ここからウェブアプリケーション用のルーティングを記述 22# index にアクセスしたときの処理 23@app.route('/') 24def index(): 25 title = "ようこそ" 26 message = picked_up() 27 # index.html をレンダリングする 28 return render_template('index.html', 29 message=message, title=title) 30 31# /post にアクセスしたときの処理 32@app.route('/post', methods=['GET', 'POST']) 33def post(): 34 title = "こんにちは" 35 if request.method == 'POST': 36 # リクエストフォームから「名前」を取得して 37 name = request.form['name'] 38 # index.html をレンダリングする 39 return render_template('index.html', 40 name=name, title=title) 41 else: 42 # エラーなどでリダイレクトしたい場合はこんな感じで 43 return redirect(url_for('index')) 44 45if __name__ == '__main__': 46 app.debug = True # デバッグモード有効化 47 app.run(host='0.0.0.0') # どこからでもアクセス可能に
宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー