質問編集履歴
2
タグの追加
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|
1
アクセス方法・エラー内容・別パターンのエラーに関して追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -25,4 +25,81 @@
|
|
25
25
|
|
26
26
|
pythonのversionは[[バッドノウハウ]webプログラミング初学者がAWS Cloud9でPython3を使う](https://qiita.com/h008/items/f3e3faeab9271ed0a1df)を参考にし、python3.6.2となっております。
|
27
27
|
|
28
|
-
解決方法をご教示願います。
|
28
|
+
解決方法をご教示願います。
|
29
|
+
|
30
|
+
**追記:**ご指摘ありがとうございます。
|
31
|
+
アプリ実行時に表示されている「http://0.0.0.0:8080/」をgoogle chromeでアクセスした結果、
|
32
|
+
|
33
|
+
> ページを表示できません
|
34
|
+
>
|
35
|
+
> サーバーに接続できません。
|
36
|
+
> サーバーが存在しないか、接続拒否している可能性があります。
|
37
|
+
|
38
|
+
と、表示されてしまいます。
|
39
|
+
|
40
|
+
また、別の話になってしまうのですが、旧cloud9(AWS版では無いもの)で別のFlaskアプリ(以下に記載)を起動させ、同様にアクセスすると
|
41
|
+
|
42
|
+
> No application seems to be running here!
|
43
|
+
> Cloud9 can't get you to your requested workspace. Here are some suggestions on how to figure out what's going on:
|
44
|
+
>
|
45
|
+
> Check that the workspace name (stockpile) and username (nokonoko123) are typed correctly.
|
46
|
+
> Check that the server is successfully running on Cloud9:
|
47
|
+
> If the server hit an error, the output window will have a message telling you what it is
|
48
|
+
> If you're in the middle of debugging code, your server might be paused right now
|
49
|
+
> The server might be running on a different port; make sure it's on port $PORT with $IP as the IP address
|
50
|
+
|
51
|
+
と表示されてアクセスできませんでした。
|
52
|
+
|
53
|
+
アプリのコードは以下のようになります。
|
54
|
+
|
55
|
+
```python
|
56
|
+
#!/usr/bin/env python
|
57
|
+
# coding: utf-8
|
58
|
+
|
59
|
+
# Flask などの必要なライブラリをインポートする
|
60
|
+
from flask import Flask, render_template, request, redirect, url_for
|
61
|
+
import random
|
62
|
+
|
63
|
+
# 自身の名称を app という名前でインスタンス化する
|
64
|
+
app = Flask(__name__)
|
65
|
+
|
66
|
+
# メッセージをランダムに表示するメソッド
|
67
|
+
def picked_up():
|
68
|
+
messages = [
|
69
|
+
"こんにちは、あなたの名前を入力してください",
|
70
|
+
"やあ!お名前は何ですか?",
|
71
|
+
"あなたの名前を教えてね"
|
72
|
+
]
|
73
|
+
# random.choice で配列からランダムに取り出し
|
74
|
+
return random.choice(messages)
|
75
|
+
|
76
|
+
# ここからウェブアプリケーション用のルーティングを記述
|
77
|
+
# index にアクセスしたときの処理
|
78
|
+
@app.route('/')
|
79
|
+
def index():
|
80
|
+
title = "ようこそ"
|
81
|
+
message = picked_up()
|
82
|
+
# index.html をレンダリングする
|
83
|
+
return render_template('index.html',
|
84
|
+
message=message, title=title)
|
85
|
+
|
86
|
+
# /post にアクセスしたときの処理
|
87
|
+
@app.route('/post', methods=['GET', 'POST'])
|
88
|
+
def post():
|
89
|
+
title = "こんにちは"
|
90
|
+
if request.method == 'POST':
|
91
|
+
# リクエストフォームから「名前」を取得して
|
92
|
+
name = request.form['name']
|
93
|
+
# index.html をレンダリングする
|
94
|
+
return render_template('index.html',
|
95
|
+
name=name, title=title)
|
96
|
+
else:
|
97
|
+
# エラーなどでリダイレクトしたい場合はこんな感じで
|
98
|
+
return redirect(url_for('index'))
|
99
|
+
|
100
|
+
if __name__ == '__main__':
|
101
|
+
app.debug = True # デバッグモード有効化
|
102
|
+
app.run(host='0.0.0.0') # どこからでもアクセス可能に
|
103
|
+
```
|
104
|
+
|
105
|
+
宜しくお願い致します。
|