栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

python cgi

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python cgi

Step 1 :安装
sudo apt-get install fcgiwrap
Step 2 :配置
    文件路径配置

vim /etc/nginx/fcgiwrap.conf

location /cgi/ {
  gzip off;
  root  ${替换为你的代码文件目录};
  fastcgi_pass  unix:/var/run/fcgiwrap.socket;
  include /etc/nginx/fastcgi_params;
  fastcgi_param script_FILENAME  ${替换为你的代码文件目录}$fastcgi_script_name;
}
    nginx增加路径入口

vim /etc/nginx/sites-available/default

server {
    listen      80;
    server_name a.yeshen.org;

    include fcgiwrap.conf; # 新增配置
}
Step 3 : 简单demo
cd ${替换为你的代码文件目录}
vim test.py
chmod a+x test.py

test.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

print "Content-type:text/html"
print
print ''
print ''
print ''
print 'Hello World'
print ''
print ''
print 'Hello World!'
print ''
print ''
Step 4: 完整小demo

index.html



  
    
    
    simple cgi
    
    

     
    
    
    

index.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

__author__ = 'hello@yeshen.org'


print "Content-type:text/html"
print "rn"

with open('index.html', 'r') as f:
    print f.read()

get.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

__author__ = 'hello@yeshen.org'

import os
import json

print "Content-type:application/json; charset=UTF-8"
print "rn"

data = {}
for key in os.environ.keys():
    data[key] = os.environ[key]

print json.dumps(data)

post.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

__author__ = 'hello@yeshen.org'

import os
import json
import cgi, cgitb


def parse_request_body(storage):
  if storage.value is not None and type(storage.value) is str:
    return json.loads(storage.value)
  else:
    return {}


def main():
  print "Content-type:application/json; charset=UTF-8"
  print "rn"

  body = parse_request_body(cgi.FieldStorage())
  if body.get("key") == 'value':
    body['key'] = "hihi"
  print json.dumps(body)


if __name__ == "__main__":
  main()

预览地址

参考:
    Nginx 上的 Python CgiPython CGI编程
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/733539.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号