python flask 用 selenium 時用 wsgi 會發生 500 錯誤
Sep 16 14:19:08 ecs-b222.novalocal uwsgi[3565]: File "/home/flask/app/env/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
Sep 16 14:19:08 ecs-b222.novalocal uwsgi[3565]: rv = self.dispatch_request()
Sep 16 14:19:08 ecs-b222.novalocal uwsgi[3565]: File "/home/flask/app/env/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
Sep 16 14:19:08 ecs-b222.novalocal uwsgi[3565]: return self.view_functions[rule.endpoint](**req.view_args)
Sep 16 14:19:08 ecs-b222.novalocal uwsgi[3565]: File "./app.py", line 260, in postJsonadd
Sep 16 14:19:08 ecs-b222.novalocal uwsgi[3565]: ret = dns_add(content)
Sep 16 14:19:08 ecs-b222.novalocal uwsgi[3565]: File "./app.py", line 80, in add_dns_add
Sep 16 14:19:08 ecs-b222.novalocal uwsgi[3565]: driver.quit()
Sep 16 14:19:08 ecs-b222.novalocal uwsgi[3565]: UnboundLocalError: local variable 'driver' referenced before assignment
Sep 16 14:19:08 ecs-b222.novalocal uwsgi[3565]: [pid: 3575|app: 0|req: 3/5] 154.92.212.0 () {54 vars in 796 bytes} [Mon Sep 16 14:19:06 2019] POST /add => generated 0 bytes in 2012 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
但是不掛 wsgi 於 使用者 flask 掛 source env/bin/activate
執行 python3 -u app.py 測過可正常運行 selenium 瀏覽網頁抓取所要的資訊
Sep 16 14:19:08 ecs-b222.novalocal uwsgi[3565]: driver.quit()
Sep 16 14:19:08 ecs-b222.novalocal uwsgi[3565]: UnboundLocalError: local variable 'driver' referenced before assignment
應該是這行的問題。
你得去檢查uwsgi用什麼帳號執行,或者乾脆在uwsgi的config裡加上
uid = flask
我是參考
https://mitchjacksontech.github.io/How-To-Flask-Python-Centos7-Apache-uWSGI/
做的 ....
[root@ecs-b222 ~]# ll /home/flask/app/wsgi.ini
-rw-rw-r-- 1 flask flask 140 Sep 16 23:48 /home/flask/app/wsgi.ini
[root@ecs-b222 ~]# cat /home/flask/app/wsgi.ini
[uwsgi]
module = wsgi
uid = flask
master = true
processes = 5
socket = 127.0.0.1:8099
chmod-socket = 660
vacuum = true
die-on-term = true
没有用要如何修改 ? (有重啟 systemctl restart flaskapp)
補充一下 看到 log ...
應該是 uWSGI 虛擬路徑內沒有 Xvfb
Can not find Xvfb. Please install it and try again.
不知要如何改 ....
終於搞定了 ... 我是用 display+firefox
Xvfb 複製到 /home/flask/app/env/bin 下面
Xvfb 的 lib 複製到 /home/flask/app/env/lib 下面(有40個)
firefox 複製到 /home/flask/app/env/bin 下面
geckodriver 複製到 /home/flask/app/env/bin 下面
您給的建議很好 ... 就是
uwsgi 用什麼帳號執行
讓我想到那是 virtualenv(虛擬環境)
就像是 chroot 的環境 ...
這小小的 虛擬環境 裡面沒有 Xvfb + firefox + geckodriver 這樣 driver 當然啟動不了
就是知道這是環境問題,才會這樣答你啊。XD
不過點了就通,不錯。