这几天抽空学习了一下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系统,要不是因为他的超长续航时间是我的刚需,我绝对不会买这样一个残次品。自己的系统都不能做主了,简直造反。