如何解决脚本标题的过早结尾以及没有此类文件或目录:“ ./。py”错误的执行程序

时间:2019-02-24 16:16:39

标签: python apache cgi mamp

我确实浏览了类似的问题,但是我的程序没有运气

这是我的程序

#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
device_id = form.getvalue('updateDeviceId')
manufact  = form.getvalue('updateManufactureId')
polling_add = form.getvalue('updatePollingAddress')
expand  = form.getvalue('updateExpDev')
loop_control = form.getvalue('updateLoopControl')
message  = form.getvalue('updateMessage')
descriptor = form.getvalue('updateDescriptor')
long  = form.getvalue('updateLongTag')
short = form.getvalue('updateShortTag')
date  = form.getvalue('updateDate')
hardware = form.getvalue('updateHardware')
software  = form.getvalue('updateSoftware')
device = form.getvalue('updateDevice')
protocol_major  = form.getvalue('updateProtocolMajor')
master_slave = form.getvalue('updateMS')
final  = form.getvalue('updateFinal')

print("Content-type: text/html")
print("")
print("")
print("The values are")
print("")
print("")
print("
Hello %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
" % (device_id, manufact, polling_add, expand, loop_control, message, 
descriptor, long, short, date, hardware, software, device, 
protocol_major, master_slave, final))
print("")
print("")

我正在使用MAMP运行apache服务器。我已经在.py文件中指定了chmod 755权限。

下面是我的httpd.conf文件

<Directory "/Applications/MAMP/cgi-bin">
    AllowOverride None
    Options +ExecCGI
    Order allow,deny
    AddHandler cgi-script .cgi .pl .py
    Allow from all
</Directory>

1 个答案:

答案 0 :(得分:0)

问题是此打印声明:

print("
Hello %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
" % (device_id, manufact, polling_add, expand, loop_control, message, 
descriptor, long, short, date, hardware, software, device, 
protocol_major, master_slave, final))

在Python中,多行字符串用triple quotes括起来:

print("""
Hello %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
""" % (device_id, manufact, polling_add, expand, loop_control, message, 
descriptor, long, short, date, hardware, software, device, 
protocol_major, master_slave, final))