質問編集履歴

2

タグの追加

2018/04/18 12:41

投稿

nokonoko_1203
nokonoko_1203

スコア17

test CHANGED
File without changes
test CHANGED
File without changes

1

アクセス方法・エラー内容・別パターンのエラーに関して追記

2018/04/18 12:41

投稿

nokonoko_1203
nokonoko_1203

スコア17

test CHANGED
File without changes
test CHANGED
@@ -53,3 +53,157 @@
53
53
 
54
54
 
55
55
  解決方法をご教示願います。
56
+
57
+
58
+
59
+ **追記:**ご指摘ありがとうございます。
60
+
61
+ アプリ実行時に表示されている「http://0.0.0.0:8080/」をgoogle chromeでアクセスした結果、
62
+
63
+
64
+
65
+ > ページを表示できません
66
+
67
+ >
68
+
69
+ > サーバーに接続できません。
70
+
71
+ > サーバーが存在しないか、接続拒否している可能性があります。
72
+
73
+
74
+
75
+ と、表示されてしまいます。
76
+
77
+
78
+
79
+ また、別の話になってしまうのですが、旧cloud9(AWS版では無いもの)で別のFlaskアプリ(以下に記載)を起動させ、同様にアクセスすると
80
+
81
+
82
+
83
+ > No application seems to be running here!
84
+
85
+ > Cloud9 can't get you to your requested workspace. Here are some suggestions on how to figure out what's going on:
86
+
87
+ >
88
+
89
+ > Check that the workspace name (stockpile) and username (nokonoko123) are typed correctly.
90
+
91
+ > Check that the server is successfully running on Cloud9:
92
+
93
+ > If the server hit an error, the output window will have a message telling you what it is
94
+
95
+ > If you're in the middle of debugging code, your server might be paused right now
96
+
97
+ > The server might be running on a different port; make sure it's on port $PORT with $IP as the IP address
98
+
99
+
100
+
101
+ と表示されてアクセスできませんでした。
102
+
103
+
104
+
105
+ アプリのコードは以下のようになります。
106
+
107
+
108
+
109
+ ```python
110
+
111
+ #!/usr/bin/env python
112
+
113
+ # coding: utf-8
114
+
115
+
116
+
117
+ # Flask などの必要なライブラリをインポートする
118
+
119
+ from flask import Flask, render_template, request, redirect, url_for
120
+
121
+ import random
122
+
123
+
124
+
125
+ # 自身の名称を app という名前でインスタンス化する
126
+
127
+ app = Flask(__name__)
128
+
129
+
130
+
131
+ # メッセージをランダムに表示するメソッド
132
+
133
+ def picked_up():
134
+
135
+ messages = [
136
+
137
+ "こんにちは、あなたの名前を入力してください",
138
+
139
+ "やあ!お名前は何ですか?",
140
+
141
+ "あなたの名前を教えてね"
142
+
143
+ ]
144
+
145
+ # random.choice で配列からランダムに取り出し
146
+
147
+ return random.choice(messages)
148
+
149
+
150
+
151
+ # ここからウェブアプリケーション用のルーティングを記述
152
+
153
+ # index にアクセスしたときの処理
154
+
155
+ @app.route('/')
156
+
157
+ def index():
158
+
159
+ title = "ようこそ"
160
+
161
+ message = picked_up()
162
+
163
+ # index.html をレンダリングする
164
+
165
+ return render_template('index.html',
166
+
167
+ message=message, title=title)
168
+
169
+
170
+
171
+ # /post にアクセスしたときの処理
172
+
173
+ @app.route('/post', methods=['GET', 'POST'])
174
+
175
+ def post():
176
+
177
+ title = "こんにちは"
178
+
179
+ if request.method == 'POST':
180
+
181
+ # リクエストフォームから「名前」を取得して
182
+
183
+ name = request.form['name']
184
+
185
+ # index.html をレンダリングする
186
+
187
+ return render_template('index.html',
188
+
189
+ name=name, title=title)
190
+
191
+ else:
192
+
193
+ # エラーなどでリダイレクトしたい場合はこんな感じで
194
+
195
+ return redirect(url_for('index'))
196
+
197
+
198
+
199
+ if __name__ == '__main__':
200
+
201
+ app.debug = True # デバッグモード有効化
202
+
203
+ app.run(host='0.0.0.0') # どこからでもアクセス可能に
204
+
205
+ ```
206
+
207
+
208
+
209
+ 宜しくお願い致します。