這幾天抽空學習了一下Python CGI開發,發現了幾個問題。
Python腳本作爲CGI腳本,傳參和普通程序差不多,只不過取參數的地方不同。
#!/usr/bin/env python3
import cgi
import dbm
import os
loginform = cgi.FieldStorage()
accountName = loginform.getvalue('name', "no_name")
print('Content-type:text/plain\n')
if accountName == 'no_name':
print('no_name')
exit(0)
#os.chdir('/mnt/disk_b/file-data/')
file = dbm.open('account', 'c')
if accountName not in file:
print('no_data')
exit(0)
controlmark = file[accountName]
# controlmark example
# password=something;access=acc,abb]bcc,add];
pairsCollection = controlmark.decode().split(';')
for pair in pairsCollection:
if 'password' in pair:
password = pair[pair.find('=')+1:]
if password == loginform.getvalue('password'):
break
pass
else:
print('passworderror')
file.close()
exit(0)
pass
for pair in pairsCollection:
if 'access' in pair:
access = pair[pair.find('=')+1:]
print(access)
file.close()
exit(0)
開發環境MacOS sierra,Python3,pycharm CE
如果註釋掉 os.chdir條目,報錯 【Errno 13】Permission denied。
花了一天時間查找問題,無果。
添加上os.chdir之後,完美運行,也就是說項目目錄下獲取不到權限。有可能是權限控制問題。
羅列權限信息
bogon:MyWebsite master$ pwd -L
/Users/master/Projects/Pycharm/MyWebsite
bogon:MyWebsite master$ ls -lR
total 16
drwsrwsrwx 5 master staff 170 6 11 15:40 cgi-bin
-rw-r--r-- 1 master staff 3973 6 11 12:52 index.html
drwxr-xr-x 8 master staff 272 6 11 12:50 index_src
-rwxr-xr-x 1 master staff 928 5 15 19:10 webserver.py
./cgi-bin:
total 24
-rwxr-xr-x 1 master staff 931 6 11 15:40 LoginProcess.py
-rwxr-xr-x 1 master staff 88 6 11 15:28 Test.py.bak
lrwxr-xr-x 1 master staff 22 5 15 19:10 file-data -> /mnt/disk_b/file-data/
./index_src:
total 152
-rw-r--r-- 1 master staff 46 6 11 10:44 DemandLoading.js
-rw-r--r-- 1 master staff 4389 6 11 12:50 MyWebsite.css
-rw-r--r-- 1 master staff 29769 6 10 16:00 Respond.js
-rwxrwxrwx 1 master staff 5738 6 10 18:40 StaticNavigate.xml
-rw-r--r-- 1 master staff 16982 5 15 19:10 SymbolicGril.jpg
-rw-r--r-- 1 master staff 3126 5 15 19:10 helpContent.html
估計是被人坑了。似乎Mac系統有什麼目錄權限保護的底層機制,必須通過什麼安全模式啓動之後,在什麼選項裏面設置,而且還不能長期有用。之前在寫cpp的時候就被坑了一次,沒想到又在這裏等着我。
無語了,所以說我討厭Mac系統,要不是因爲他的超長續航時間是我的剛需,我絕對不會買這樣一個殘次品。自己的系統都不能做主了,簡直造反。