利用 Python 的 CGIHTTPServer 测试 CGI 脚本程序

Python 3

步骤:

  1. 新建HttpServer目录

    1
    2
    mkdir HttpServer
    cd HttpServer
  2. 在HttpServer目录下新建cgi-bin目录

    1
    2
    mkdir cgi-bin
    cd cgi-bin
  3. 在cgi-bin目录下新建helloworld.sh文件,内容如下:

1
vim helloworld.sh
1
2
3
4
5
#!/bin/bash
echo "Content-Type:text/html"
echo ""

echo "hello world!"
  1. 在cgi-bin目录下打开命令行,执行
1
chmod +x helloworld.sh

赋予helloworld.sh可执行的权限

  1. 在HttpServer目录下打开命令行,执行命令
1
python -m http.server --cgi --bind 0.0.0.0 8088

就会把HttpServer目录以后台服务的方式作为CGIHTTPServer启动,运行log可在当前目录的nohup.out文件中查看。

  1. 打开浏览器,地址栏出入:

http://localhost:8088/cgi-bin/helloworld.sh

即可看到输出的Hello World!

Python 2

步骤:

  1. 新建HttpServer目录

  2. 在HttpServer目录下新建cgi-bin目录

  3. 在cgi-bin目录下新建helloworld.sh文件,内容如下:

1
2
3
4
5
#!/bin/bash
echo "Content-Type:text/html"
echo ""

echo "hello world!"
  1. 在cgi-bin目录下打开命令行,执行
1
chmod +x helloworld.sh

赋予helloworld.sh可执行的权限

  1. 在HttpServer目录下打开命令行,执行命令
1
nohup python -m CGIHTTPServer 8088 &

就会把HttpServer目录以后台服务的方式作为CGIHTTPServer启动,运行log可在当前目录的nohup.out文件中查看。

  1. 打开浏览器,地址栏出入:

http://localhost:8088/cgi-bin/helloworld.sh

即可看到输出的Hello World!

参考

利用Python的CGIHTTPServer测试CGI脚本程序

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. Python 3
  2. 2. Python 2
  3. 3. 参考
,