0
点赞
收藏
分享

微信扫一扫

python cgi

得一道人 2022-02-13 阅读 93

Step 1 :安装

sudo apt-get install fcgiwrap

Step 2 :配置

  1. 文件路径配置

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;
}
  1. 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 '<html>'
print '<head>'
print '<meta charset="utf-8">'
print '<title>Hello World</title>'
print '</head>'
print '<body>'
print '<h2>Hello World!</h2>'
print '</body>'
print '</html>'

Step 4: 完整小demo

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>simple cgi</title>
    <meta name="author" content="hello@yeshen.org">
    <style type="text/css">
    button { width: 80px; height: 44px;border: 1px solid #ccc;border-radius: 4px;}
    </style>
</head>
<body>     
    <button onclick="get()">get</button>
    <button onclick="post()">post</button>
    <div id="ret"></div>
<script type="text/javascript">

var get_url = '/cgi/get.py?key=value'
var post_url = '/cgi/post.py'

function get(mode){
  fetch(get_url).then(res => res.text()).then(ret => {
    console.log('done',get_url,ret);
    document.getElementById('ret').textContent = ret;
  });
}
function post(){
  fetch(post_url, {
    method: 'POST',
    body: JSON.stringify({ "key" : "value"}),
    headers: { 'Content-Type': 'application/json'}
  }).then(res => res.text()).then(ret => { 
    console.log('done',post_url,ret)
    document.getElementById('ret').textContent = ret;
  });
}
</script>
</body>
</html>

index.py

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

__author__ = 'hello@yeshen.org'


print "Content-type:text/html"
print "\r\n"

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 "\r\n"

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 "\r\n"

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


if __name__ == "__main__":
  main()

预览地址

参考:

  1. Nginx 上的 Python Cgi
  2. Python CGI编程
举报

相关推荐

0 条评论