美文网首页
08.Web服务器-3.Web静态服务器

08.Web服务器-3.Web静态服务器

作者: JerryChenn07 | 来源:发表于2018-09-24 17:22 被阅读5次

    1、显示固定的页面

    from socket import *
    from multiprocessing import *
    import os
    
    
    def handleClient(clientSocket):
        #用一个新的进程,为一个客户端进行服务
        data = clientSocket.recv(1024).decode('utf-8')
        print(data)
    
        responseHeaderLines = "HTTP/1.1 200 OK"+os.linesep
        responseHeaderLines += "k1:v1"+os.linesep
        responseHeaderLines += "k2:v2" + os.linesep
        responseHeaderLines += os.linesep
        responseBody = "hello <h1>老王八</h1>"
        response = responseHeaderLines + responseBody
    
        clientSocket.send(response.encode('utf-8'))
        clientSocket.close()
    
    
    if __name__ == '__main__':
        #作为程序的主控制入口
        serverSocket = socket(AF_INET, SOCK_STREAM)
        serverSocket.bind(("", 82))
        serverSocket.listen()
        while True:
            print('服务端等待客户端连接......')
            clientSocket, clientAddr = serverSocket.accept()
            Process(target=handleClient, args=(clientSocket,)).start()
            print('%s-%s 连接成功......' % (clientAddr[0], clientAddr[1]))
    
    

    2、显示需要的页面

    from socket import *
    from multiprocessing import *
    import os
    
    # 设置静态文件根目录
    HTML_ROOT_DIR = "./html"
    
    
    def handleClient(clientSocket):
        data = clientSocket.recv(1024)
        data = data.decode('utf-8')
        """解析请求报文"""
        # 提取用户请求的文件名,如果是get请求携带参数要处理?
        file_name = data.splitlines()[0].split(' ')[1]
    
        #如果是/一般表示请求首页
        if "/" == file_name:
            file_name = "/index.html"
    
        # 打开文件,读取内容
        try:
            #模拟一个运行中的异常
            #num=1/0
            with open(HTML_ROOT_DIR + file_name, "rb") as file:
                file_data = file.read()
            # 构造响应数据
            response_start_line = "HTTP/1.1 200 OK"+os.linesep
            response_headers = "Server: My server"+os.linesep
            response_body = file_data.decode("utf-8")
        except FileNotFoundError:
            response_start_line = "HTTP/1.1 404 Not Found"+os.linesep
            response_headers = "Server: My server"+os.linesep
            with open(HTML_ROOT_DIR + '/404.html', "r",encoding='utf-8') as file:
                response_body = file.read()
        except:
            response_start_line = "HTTP/1.1 500 ERROR" + os.linesep
            response_headers = "Server: My server" + os.linesep
            with open(HTML_ROOT_DIR + '/error.html', "r",encoding='utf-8') as file:
                response_body = file.read()
    
        response_headers += "Content-Type:text/html;charset=utf-8" + os.linesep
    
        response = response_start_line + response_headers + os.linesep + response_body
        #print("response data:", response)
    
        # 向客户端返回响应数据
        clientSocket.send(response.encode('utf-8'))
        # 关闭客户端连接
        clientSocket.close()
    
    
    if __name__ == '__main__':
        """作为程序的主控制入口"""
        serverSocket = socket(AF_INET, SOCK_STREAM)
        serverSocket.bind(("", 6788))
        serverSocket.listen()
        while True:
            print('服务端等待客户端连接......')
            clientSocket, clientAddr = serverSocket.accept()
            Process(target=handleClient, args=(clientSocket,)).start()
            print('%s-%s 连接成功......' % (clientAddr[0], clientAddr[1]))
    
    

    3、符合面向对象思想设计类实现

    from socket import *
    from multiprocessing import *
    import os
    
    # 设置静态文件根目录
    HTML_ROOT_DIR = "./html"
    
    
    
    class MyHttpServer(object):
        """服务器类"""
        def __init__(self):
            """初始化,创建了一个TCP服务端对象"""
            self.server_socket = socket(AF_INET, SOCK_STREAM)
    
        def bind(self, port=5678):
            """绑定端口号"""
            self.server_socket.bind(("", port))
    
        def start(self):
            """开始监听"""
            self.server_socket.listen()
            while True:
                client_socket, client_address = self.server_socket.accept()
                print("[%s, %s]用户连接上了" % client_address)
                Process(target=self.handle_client, args=(client_socket,)).start()
    
        def handle_client(self,client_socket):
            """处理客户端的请求"""
            data = client_socket.recv(1024)
            data = data.decode('utf-8')
            """解析请求报文"""
            # 提取用户请求的文件名,如果是get请求携带参数要处理?
            file_name = data.splitlines()[0].split(' ')[1]
    
            # 如果是/一般表示请求首页
            if "/" == file_name:
                file_name = "/index.html"
    
            # 打开文件,读取内容
            try:
                # 模拟一个运行中的异常
                # num=1/0
                with open(HTML_ROOT_DIR + file_name, "rb") as file:
                    file_data = file.read()
                # 构造响应数据
                response_start_line = "HTTP/1.1 200 OK" + os.linesep
                response_headers = "Server: My server" + os.linesep
                response_body = file_data.decode("utf-8")
            except FileNotFoundError:
                response_start_line = "HTTP/1.1 404 Not Found" + os.linesep
                response_headers = "Server: My server" + os.linesep
                with open(HTML_ROOT_DIR + '/404.html', "r", encoding='utf-8') as file:
                    response_body = file.read()
            except:
                response_start_line = "HTTP/1.1 500 ERROR" + os.linesep
                response_headers = "Server: My server" + os.linesep
                with open(HTML_ROOT_DIR + '/error.html', "r", encoding='utf-8') as file:
                    response_body = file.read()
    
            response_headers += "Content-Type:text/html;charset=utf-8" + os.linesep
    
            response = response_start_line + response_headers + os.linesep + response_body
            # print("response data:", response)
    
    
            client_socket.send(response.encode('utf-8'))
            client_socket.close()
    
    
    
    if __name__ == '__main__':
        myHttpServer = MyHttpServer()
        myHttpServer.bind()
        myHttpServer.start()
    

    相关文章

      网友评论

          本文标题:08.Web服务器-3.Web静态服务器

          本文链接:https://www.haomeiwen.com/subject/ykhhoftx.html