cike0415 发表于 2018-8-12 08:17:45

python CGI 文件读写注意 500

  cgi出现500错误 ,是由于 HTTP表头不对
gi的通信依靠stdout与浏览器通信。  所以简单地在py-cgi-index.py里面写:
  #!/usr/bin/env python
  print 'hello world'
  这样写是不对的。
  cgi接口规定,cgi脚本输出的开头应该是http header。
  而hello world这种字符无法被识别为任何有效的http header,
  所以如果访问http://localhost/python-cgi,会返回500错误。
  解决办法有两个:
  1、写上http header。
  header与body之间必须有一个空行,以识别前面的是header,后面的是body。
  代码改成:
  #!/usr/bin/env python
  print 'Content-Type: text/html\n\nhello world'
  2、空白http header。
  不写http header的情况下,apache会自动补上header。
  代码改成:
  #!/usr/bin/env python
  print '\nhello world'
  cgi要读写文件,注意一定要将该文件权限设置为666,即全部可写。
页: [1]
查看完整版本: python CGI 文件读写注意 500