Python实现简单的Web服务器 Part2—支持动态网站

1. 什么是CGI?

CGI即通用网关接口(Common Gateway Interface),是外部应用程序(CGI程序)与Web服务器之间的接口标准,是在CGI程序和Web服务器之间传递信息的规程。CGI规范允许Web服务器执行外部程序,并将它们的输出发送给Web浏览器,CGI将Web的一组简单的静态超媒体文档变成一个完整的新的交互式媒体。

下面是我个人对CGI的理解

就是最早的web服务器,只能处理静态页面的响应,比如 localhost/index.html 。但是有的时候,浏览器需要和服务器进行数据的交互,是一个动态的网页。

比如说,浏览器提交了账号和密码,提交给服务器,服务器收到数据之后,然后返回一个计算的结果。这就是动态的页面了。

比如,输入一个网页是 localhost/.index.php,localhost/index.jsp,localhost/index.py 。

比如说,写php的时候,服务器的目录下面可能会有这样的文件,文件名是 index.php

<!DOCTYPE html> 
<html> 
<body> 
<?php 
echo "Hello World!"; 
?> 
</body> 
</html> 

当浏览器请求 localhost/index.php 这个文件的时候,其实服务器并不会立刻把这个文件返回给服务器,它会先在自己这边执行一下,然后把执行的结果返回给服务器。

这就是我理解的CGI。
<br />

下面想写得是,在这个简单的web服务器上去支持cgi。

比如说,当浏览器输入 localhost/time.py 的时候,返回的不是一个静态的固定的网页,而是能够返回当前的时间。这个就需要服务器能够支持对动态网页的响应。也就是说,服务器收到浏览器的请求之后,先在服务器端执行一下进程,然后在把执行的结果返回。

运行结果 是这样


image.png
http 命令 是一个好工具 !

增加了对脚本文件的处理

class case_cgi_file(object):
    '''脚本文件处理'''
    #判斷一下當前的服務器上有沒有這個文件
    def test(self, requesthandler):
        return os.path.isfile(requesthandler.full_path) and \
            requesthandler.full_path.endswith('.py')

    #如果有的話,就去執行
    def act(self, requesthandler):
        ##运行脚本文件
        self.run_cgi(requesthandler)

    #去執行這個python腳本,執行之後將結果返回
    def run_cgi(self,requesthandler):
        data = subprocess.check_output(["python", requesthandler.full_path])
        #將執行的結果返回個瀏覽器
        requesthandler.send_content(data)

这是全部的代码:

#coding:utf-8
import sys,os,BaseHTTPServer
from datetime import datetime
import subprocess

class case_cgi_file(object):
    '''脚本文件处理'''
    #判斷一下當前的服務器上有沒有這個文件
    def test(self, requesthandler):
        return os.path.isfile(requesthandler.full_path) and \
            requesthandler.full_path.endswith('.py')

    #如果有的話,就去執行
    def act(self, requesthandler):
        ##运行脚本文件
        self.run_cgi(requesthandler)

    #去執行這個python腳本,執行之後將結果返回
    def run_cgi(self,requesthandler):
        data = subprocess.check_output(["python", requesthandler.full_path])
        #將執行的結果返回個瀏覽器
        requesthandler.send_content(data)


#RequestHandler 繼承 BaseHTTPRequestHandler ,所以他自身就有一個path的數據成員
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    Error_Page="""\
    <html>
    <body>
    <h1>Error accessing {path}</h1>
    <p>{msg}</p>
    </body>
    </html>
    """
    Cases=[case_cgi_file()]

    #這裏的self其實是  RequestHandler對象
    def do_GET(self):
        try:
            self.full_path = os.getcwd() + self.path
            for case in self.Cases:
                if case.test(self):
                    case.act(self)
                    break
        except Exception as msg:
            self.handle_error(msg)

    def handle_error(self,msg):
        content=self.Error_Page.format(path=self.path,msg=msg)
        self.send_content(content,404)

    def send_content(self,page,status=200):
        #print 'send_content function '
        self.send_response(status)
        self.send_header('Content-Type','text/html')
        self.send_header('Content-Length','text.html')
        self.end_headers()
        self.wfile.write(page)



if __name__ == '__main__':
    serverAddress = ('', 8081)
    server = BaseHTTPServer.HTTPServer(serverAddress, RequestHandler)
    server.serve_forever()

gitbub 链接:https://github.com/zhaozhengcoder/Python

<br />

关于python 创建一个子线程

去执行其他的可执行文件的用法。这个很像linux下的 exec()函数。如果感兴趣的话,可以去深入了解subprocess库。

import subprocess
path='./echo.py'
res=subprocess.check_output(["python",path])
print res
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容