网页服务器开发:CGI

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值