LoginSignup
2
0

More than 5 years have passed since last update.

Now.sh (Zeit.co) で Python Flask がエラーで動かないときの対処法

Last updated at Posted at 2019-03-28

Now.sh (Zeit.co) 上で Python で Flask を使って LINE Bot を作ろうとするもエラーで動かなかったのでメモ。
Now.sh を使った LINE Bot の作り方については別記事へ
→ https://qiita.com/shge/items/06169220a8f55e2ed861

前提

  • ローカルで動かしてみて動くことを確認
  • requirements.txt で必要なパッケージを指定
  • now.json(Now の設定ファイル)の指定
now.json
{
  "version": 2,
  "builds": [
    { "src": "*.py", "use": "@now/python" }
  ]
}
  • @app.route() を使用する場合は、now.jsonroutes を設定しないと 404 Not Found になる

https://zeit.co/docs/v2/deployments/configuration#routes
例:

now.json
{
  "version": 2,
  "builds": [
    { "src": "*.py", "use": "@now/python" }
  ],
  "routes": [
    { "src": "/.*", "dest": "/"}
  ]
}

問題

以下のように request のデータを得ようとするとエラーが発生する(多分)。

request.get_data()
request.form["name"]

ログ(***.now.sh/_logs から見れる):

----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 34056)
Traceback (most recent call last):
  File "/var/lang/lib/python3.6/socketserver.py", line 320, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/var/lang/lib/python3.6/socketserver.py", line 351, in process_request
    self.finish_request(request, client_address)
  File "/var/lang/lib/python3.6/socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
TypeError: 'WebhookHandler' object is not callable
----------------------------------------
('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',)): ConnectionError
Traceback (most recent call last):
  File "/var/task/now__handler__python.py", line 29, in now_handler
    headers=headers, data=body, allow_redirects=False)
  File "/var/task/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/var/task/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/var/task/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/var/task/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

そこでググると以下のページがヒットした。
https://github.com/zeit/now-examples/issues/163
@liudonghua123/now-flask を使えとあるのでその通りにするが…

{
  "version": 2,
  "builds": [
-    { "src": "*.py", "use": "@now/python" }
+    { "src": "*.py", "use": "@liudonghua123/now-flask" }
  ]
}

今度はタイムアウト…

Task timed out after 10.01 seconds

https://github.com/zeit/now-builders/pull/163 を見つけ @rmnclmnt/python-wsgi で試すも、今度は redirect() がうまく作動せずタイムアウトエラー。
そこでこの Pull Request の作成者の方に聞いてみたところ、@sisp/python-wsgi を使うといいよと教えてくれたのでやってみると…

解決法

now.json
{
  "version": 2,
  "builds": [
    { "src": "*.py", "use": "@sisp/python-wsgi" }
  ]
}

動いた!! :tada::tada:

まとめ

Now.sh の Python の公式ビルダーはまだアルファ版らしいので、まだ merge されていない有志の方が作ったビルダーを使うとうまくいった。

追記 (2019/04/03)

無事マージされ @now/python-wsgi で使えるようになった模様です。

現在開発中:https://github.com/zeit/now-builders/pull/339

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0