示例#1
0
    def service_client(self, new_socket):
        """为这个客户端返回数据"""

        # 1. 接收浏览器发送过来的请求,即http请求
        # GET / HTTP/1.1
        request = new_socket.recv(1024).decode("utf-8")
        # print(request)
        request_lines = request.splitlines()
        print("")
        print(">>>" * 10)
        print(request_lines)

        file_name = ""
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
        if ret:
            file_name = ret.group(1)
            # print("*" * 10 + file_name)
            if file_name == "/":
                file_name = "/index.html"

        # 如果不是以.py结尾的就是静态资源,列入html
        if not file_name.endswith(".py"):
            try:
                file = open("./html" + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "NOT FOUND!"
                new_socket.send(response.encode("utf-8"))
            else:
                content = file.read()
                file.close()
                # 2. 返回http格式的数据给浏览器
                # 2.1 准备发送给浏览器的数据---header
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                # 2.2 准备发送给浏览器的数据---body
                # 发header
                new_socket.send(response.encode("utf-8"))
                # 发body
                new_socket.send(content)

        # 如果是.py结尾的, 就是动态资源
        else:

            env = dict()
            body = mini_frame.application(env, self.set_response_header)

            header = "HTTP/1.1 %s\r\n" % self.status
            for temp in self.headers:
                header += "%s:%s\r\n" % (temp[0], temp[1])

            header += "\r\n"

            response = header + body

            # 发送response给浏览器
            new_socket.send(response.encode("utf-8"))

        new_socket.close()
示例#2
0
 def service_client(slef, new_socket):
     #接受http请求GET / HTTP/1.1
     request = new_socket.recv(1024).decode('utf-8')
     re_list = request.splitlines()[0]
     name = ''
     name += re.match(r'[^/]+(/[^ ]*)', re_list).group(1)
     if name == '/':
         name = '/index.html'
     print(request)
     #返回http数据
     #如果返回请求资源不是以.py结尾,那么就认为是静态资源
     if not name.endswith(".py"):
         response = 'HTTP/1.1 200 OK\r\n'
         response += '\r\n'
         try:
             f = open("./html" + name, 'rb')
         except:
             response = "HTTP/1.1 404 NOT FOUND\r\n"
             response += "\r\n"
             response += "------file not found------"
             new_socket.send(response.encode("utf-8"))
         else:
             body = f.read()
             f.close()
             new_socket.send(response.encode('utf-8'))
             new_socket.send(body)
     else:
         response = "HTTP/1.1 200 OK\r\n"
         response += "\r\n"
         body = mini_frame.application(name)
         response += body
         new_socket.send(response.encode("utf-8"))
     new_socket.close()
示例#3
0
    def response_dynamic(self, file, client_socket):
        res_header = "HTTP/1.1 200 OK\r\n\r\n"  # 应答头和应答体之间空一行;为了兼容windows换行用\r\n表示
        client_socket.send(
            res_header.encode("utf-8"))  # 可以先回复头,在socket.close()之前再回复body

        body = mini_frame.application(file)
        client_socket.send(body.encode("utf-8"))
示例#4
0
    def handle_client(client_socket):
        """为一个客户端服务"""
        # print("当前子进程pid为:", os.getpid())
        # 1.接收浏览器发送的请求
        recv_data = client_socket.recv(1024).decode("utf-8")
        # 2.获取请求的url \S+匹配GET POST PUT DEL等  \s匹配空白字符 (\S+)匹配url \s匹配空白字符
        url = re.match(r"^\S+\s(\S+)\s", recv_data).group(1)
        # 3.组装应答头和应答体

        # 3.1 如果浏览器请求的是动态资源
        if url.endswith(".py"):
            response_header = "HTTP/1.1 200 0K \r\n\r\n".encode("utf-8")
            response_body = mini_frame.application(url).encode("utf-8")

        # 3.2 如果浏览器请求的是静态资源,如html/css/js/png/gif等
        else:
            # 设置默认页面,如果url为/,则跳转到index.html
            if url == "/":
                url = "/index.html"
            # 组装
            try:  # 尝试打开文件
                with open(BASE_DIR + url,
                          mode="rb") as f:  # 必须以rb的形式读取,因为有时传输的是图片
                    response_body = f.read()
            except Exception:  # 如果出现异常
                response_header = "HTTP/1.1 404 Error \r\n\r\n".encode("utf-8")
                response_body = "<h1>404 Page Not Found</h1>".encode("utf-8")
            else:  # 如果没有异常
                response_header = "HTTP/1.1 200 0K \r\n\r\n".encode("utf-8")
        # 拼接应答
        response = response_header + response_body
        # 4.返回应答
        client_socket.send(response)
        # 5.关闭套接字
        client_socket.close()
示例#5
0
    def service_client(self, new_socket):
        """为这个客户端返回数据"""
        # 1.接收浏览器发送过来的请求,即http请求
        # GET / HTTP/1.1
        # ...
        request = new_socket.recv(1024).decode("utf-8")
        # print(">>>"*50)
        # print(request)
        request_lines = request.splitlines()
        print("")
        print(">" * 20)
        print(request_lines)

        # GET /index.html HTTP/1.1  进行正则匹配
        file_name = ""
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
        if ret:
            file_name = ret.group(1)
            # print("*"*50, file_name)
            if file_name == "/":
                file_name = "/index.html"

        # 2.返回http格式的数据给浏览器
        # 2.1 如果请求的功能不是以.py结尾,那么就认为是静态资源(html/css/js/png/jpg等)
        if not file_name.endswith(".py"):
            try:
                f = open("./html" + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "---file not found---"
                new_socket.send(response.encode("utf-8"))
            else:
                html_content = f.read()
                f.close()
                # 2.1 准备发送给浏览器的数据---header
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                # 2.2 准备发送给浏览器的数据---body
                new_socket.send(response.encode("utf-8"))
                new_socket.send(html_content)
        else:
            # 2.2 如果是以.py结尾,那么就认为是动态请求
            env = dict()
            body = mini_frame.application(env, self.set_response_header)

            header = "HTTP/1.1 %s\r\n" % self.status

            for temp in self.headers:
                header += "%s:%s\r\n" % (temp[0], temp[1])

            header += "\r\n"

            response = header + body
            # 发送response给浏览器
            new_socket.send(response.encode("utf-8"))

        # 3.关闭套接字
        new_socket.close()
    def service_client(self, client_socket):
        # 接收客户端发送的请求数据
        request_data = client_socket.recv(1024).decode("utf-8")
        # print(request_data)

        # 把请求信息切割,得到一个列表
        data_lines = request_data.splitlines()
        # print(data_lines)

        # 使用正则,提取请求的文件名
        # GET /info.html HTTP/1.1
        # [^ ]表示非空,[]搭配^可以表示取反操作
        try:
            ret = re.match(r"[^/]+/([^ ]*)", data_lines[0]).group(1)
            if ret:
                file_name = ret
            else:
                file_name = 'info.html'
        except Exception:
            file_name = 'info.html'

        # 根据文件名读取数据
        print(file_name)
        if not file_name.endswith('.py'):
            # 请求静态资源
            try:
                with open("./html/" + file_name, "rb") as f:
                    file_content = f.read()
            except Exception as e:
                print(e)
                # 文件不存在返回404
                response = 'HTTP/1.1 404 NOT FOUND\r\n'
                response += "\r\n File Not Found!"
                client_socket.send(response.encode("utf-8"))
            else:
                # 根据请求数据返回页面
                # windows中\r\n表示换行
                response = 'HTTP/1.1 200 OK\r\n'
                response += "\r\n"
                # 返回两次,区分响应头和相应内容
                client_socket.send(response.encode("utf-8"))
                client_socket.send(file_content)
        else:
            # 请求动态资源
            # header = 'HTTP/1.1 200 OK\r\n\r\n'
            # body = "success %s" % time.ctime()
            # response = header + body
            # client_socket.send(response.encode('utf-8'))

            # 将判断返回何种资源的代码分离出去,达到解耦目的
            header = 'HTTP/1.1 200 OK\r\n\r\n'
            body = mini_frame.application(file_name)
            response = header + body
            client_socket.send(response.encode('utf-8'))

        # 关闭套接字
        client_socket.close()
示例#7
0
    def service_client(self, new_socket):
        """为这个客户端返回数据"""

        # 1. 接收浏览器发送过来的请求 ,即http请求
        # GET / HTTP/1.1
        # .....
        request = new_socket.recv(1024).decode("utf-8")
        # print(">>>"*50)
        # print(request)

        request_lines = request.splitlines()
        print("")
        print(">" * 20)
        print(request_lines)

        # GET /index.html HTTP/1.1
        # get post put del
        file_name = ""
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
        if ret:
            file_name = ret.group(1)
            # print("*"*50, file_name)
            if file_name == "/":
                file_name = "/index.html"

        # 2. 返回http格式的数据,给浏览器
        if not file_name.endswith(".py"):
            try:
                f = open("./html" + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "------file not found-----"
                new_socket.send(response.encode("utf-8"))
            else:
                html_content = f.read()
                f.close()
                # 2.1 准备发送给浏览器的数据---header
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                # 2.2 准备发送给浏览器的数据---boy
                # response += "hahahhah"

                # 将response header发送给浏览器
                new_socket.send(response.encode("utf-8"))
                # 将response body发送给浏览器
                new_socket.send(html_content)
        else:
            header = "HTTP/1.1 200 OK\r\n"
            header += "\r\n"

            body = mini_frame.application(file_name)
            response = header + body
            new_socket.send(response.encode("utf-8"))
        # 关闭套接
        new_socket.close()
示例#8
0
    def service_client(self, client):
        #接收流览器发过来的请求
        #GET / HTTP/1.1
        #.......
        request = client.recv(1024).decode('utf-8')

        print(request)
        print(">>>" * 50)
        request_lines = request.splitlines()
        #print(request.decode('utf-8'))

        file_name = ""
        #如果发送数据空,则直接结束
        #正则匹配TCP头的参数
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
        #如果ret不为空,返回后缀
        if ret:
            file_name = ret.group(1)
            if file_name == "/":
                file_name = "/index.html"

        #返回浏览器请求的数据

        #打开客户请求的页面
        #非.py结尾的请求资源则认为是静态资源(html\css\js等)
        if not file_name.endswith(".py"):
            try:
                f = open("./html" + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "------------not found------------"
                client.send(response.encode('utf-8'))
            else:
                html_content = f.read()
                f.close()
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"

                client.send(response.encode('utf-8'))
                client.send(html_content)
        else:
            #如果是以.py结尾的请求资源,则认为是动态资源
            header = "HTTP.1.1 200 OK\r\n"
            header += "\r\n"

            #body = "hello,world %s" % time.ctime()
            body = mini_frame.application(file_name)

            response = header + body

            client.send(response.encode('utf-8'))

        #关闭套接字
        client.close()
    def service_client(self, new_socket):
        """为这个客户端返回数据"""
        # 1. 接收浏览器发送过来的请求,即http请求
        # GET / HTTP/1.1
        # .....
        request = new_socket.recv(1024).decode("utf-8")
        # print(request)
        request_lines = request.splitlines()
        print(request_lines)
        # GET /index.html HTTP/1.1
        file_name = ""
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
        if ret:
            file_name = ret.group(1)
            # print('*'*50, file_name)
            if file_name == "/":
                file_name = "/index.html"
        # 2. 返回http格式的数据给浏览器
        # 2.1 如果请求资源不是以。py结尾,那么就认为是静态资源(html/css/js/png/jpg等)
        if not file_name.endswith(".py"):
            try:
                f = open("../html/" + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "------file not found-----"
                new_socket.send(response.encode("utf-8"))
            else:
                html_content = f.read()
                f.close()
                # 2.1 准备发送给浏览器的数据--header
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                # 2.2 准备发送给浏览器的数据-- body
                # response += "<h1>方安琪小番茄</h1>"
                # 将response header发送给浏览器
                new_socket.send(response.encode("utf-8"))
                # 将response body发送给浏览器
                new_socket.send(html_content)
        else:
            # 2.2 如果是以。py结尾,那么就认为是动态资源的请求
            header = "HTTP/1.1 200 OK\r\n"
            header += "\r\n"

            # body = "hahahaha%s"% time.ctime()
            body = mini_frame.application(file_name)
            # print("-----------保驾护航----------")
            # print(body)
            # print("-----------保驾护航----------")
            response = header + body
            # 发送response给浏览器
            new_socket.send(response.encode("utf-8"))

        # 关闭套接字
        new_socket.close()
示例#10
0
    def work(self, client_socket, client_addr):
        # 接收对方发送过来的数据
        recv_data = client_socket.recv(1024).decode("gbk")  # 接收1024个字节
        # 解析请求的页面名字
        ret = r"^GET (/.*?) HTTP"
        page_name = re.findall(ret, recv_data)
        print('请求的页面为:', page_name)

        if page_name:
            page_name = page_name[0]
            if page_name == '/':
                page_name = "/index.html"   # 如果返回的是/,则让网址访问index.html
        # 2. 返回http格式的数据,给浏览器
        # 2.1 如果请求的资源不是以.py结尾,那么就认为是静态资源(html,css,js,png,jpg等等)
        if not page_name.endswith('.py'):
            # 打开文件操作及其危险,因此在此尝试打开文件
            try:
                # 拼接地址
                root_path = r'../html'   # 根目录
                complete_page_path = root_path + page_name    # 拼接
                # 打开页面,并读取内容
                f = open(complete_page_path, 'rb')   # 打开文件
            except:   # 如果打开文件失败,则返回404
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "------file not found-----"
                client_socket.send(response.encode("utf-8"))
            else:
                body = f.read()
                f.close()
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                # body = "<h1>你好!</h1>\r\n"
                # return_data = response + body
                # 发送一些数据到客户端
                client_socket.send(response.encode('utf-8'))
                client_socket.send(body)
        else:
            # 如果是以.py结尾,那么就认为是动态请求

            # body = 'hahaha %s' % time.localtime()
            env = dict()
            env['PATH_INFO'] = page_name
            body = mini_frame.application(env, self.set_response_header)

            header = 'HTTP/1.1 %s\r\n' % self.status
            for temp in self.headers:
                header += "%s:%s\r\n" % temp
            header += "\r\n"

            response = header + body
            client_socket.send(response.encode('utf-8'))

        client_socket.close()
        print('---- 客户%s服务完毕 ----' % str(client_addr))
    def service_client(self, new_socket):
        """为这个客户端返回数据"""
        # 1. 接收浏览器发送过来的HTTP请求
        # GET / HTTP/1.1
        request = new_socket.recv(1024).decode("utf-8")
        request_lines = request.splitlines()
        print("")
        print(">"*20)
        print(request_lines)
        # GET /index.html HTTP/1.1
        file_name = ""
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
        if ret:
            file_name = ret.group(1)
            if file_name == "/":
                file_name = "/index.html"
        # 2. 返回HTTP格式的数据给浏览器
        # 如果请求的资源不是以.py结尾,那么就认为是静态资源
        if not file_name.endswith(".py"):
            try:
                f = open("./html" + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "------file not found------"
                new_socket.send(response.encode("utf-8"))
            else:
                html_content = f.read()
                f.close()
                # 2.1 准备发送给浏览器的数据---header
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                # 2.1 准备发送给浏览器的数据---body
                # response += "hahahah"

                # 将response header发送给浏览器
                new_socket.send(response.encode("utf-8"))
                # 将response body发送给浏览器
                new_socket.send(html_content)
        else:
            # 如果请求的资源以.py结尾,那么就认为是动态资源
            header = "HTTP/1.1 200 OK\r\n"
            header += "\r\n"
            # body = "haha %s" % time.ctime()
            # if file_name == "/login.py":
            #     body = mini_frame.login()
            # elif file_name == "/register.py":
            #     body = mini_frame.register()
            body = mini_frame.application(file_name)
            
            response = header + body
            new_socket.send(response.encode("utf-8"))

        # 3. 关闭套接字
        new_socket.close()
示例#12
0
    def service_client(new_socket):
        """为这个客户端返回数据"""
        # 1.接收浏览器发送过来请求,即Http请求
        request = new_socket.recv(1024).decode()
        # print(request)
        request_lines = request.splitlines()
        print(request_lines[0])

        file_name = re.findall(" (.+) HTTP/1.1", request_lines[0])[0]
        # print(request_page)
        if file_name == "/":
            file_name = "/index.html"
        file_name = file_name[1:]
        print(file_name)

        # 2.返回ht格式的数据,给浏览器
        if not file_name.endswith(".py"):
            # py 结尾为动态
            # noinspection PyBroadException
            try:
                with open(r".\html\\" + file_name, "rb") as f:
                    html_content = f.read()
            except Exception:
                # 无法访问的页面时
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "------file not found------"
                html_content = response.encode("utf-8")
            else:
                # 2.1准备发送给浏览器的数据--header
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                # 2.2 准备发送给浏览器的数据---boy
                new_socket.send(response.encode("utf-8"))
            new_socket.send(html_content)

        else:
            # 2.2 如果。py
            response = "HTTP/1.1 200 OK\r\n"
            response += "\r\n"

            # body = "hello world {}".format(time.ctime())
            # print(file_name)
            # if file_name == "login.py":
            #     body = mini_frame.login()
            # elif file_name == "register.py":
            #     body = mini_frame.register()

            body = mini_frame.application(file_name)
            # 准备发送给浏览器的数据---boy
            response += body
            new_socket.send(response.encode("utf-8"))

        # 3.关闭套接字
        new_socket.close()
    def service_client(self, new_socket: socket.socket):
        # 为这个客户端返回数据
        # 1. 响应浏览器发送过来的HTTP请求
        request = new_socket.recv(1024)
        print(request)
        request_lines = request.splitlines()
        print("")
        print(">" * 20)
        print(request_lines)
        file_name = str()
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0].decode("gbk"))
        print(ret)
        if ret:
            print(ret)
            file_name = ret.group(1)
            print(
                "-----------------------------------------------------------------------------"
            )
            print(file_name)
            print(
                "------------------------------------------------------------------------------"
            )
            if file_name == "/":
                file_name = "/index.html"
        # 2. 返回HTTP格式的数据
        if not file_name.endswith(".py"):
            try:
                file_to_open = "./html" + file_name
                print(file_to_open)
                f = open(file_to_open, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND \r\n"
                response += "\r\n"
                response += "-----------file not found---------- "
                new_socket.send(response.encode("gbk"))
            else:
                html_content = f.read()
                f.close()
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                new_socket.send(response.encode("gbk"))
                new_socket.send(html_content)
        else:
            header = "HTTP/1.1 "
            header += self.status
            header += "\r\n"

            print(header)
            body = mini_frame.application({"test": "empty"},
                                          self.set_start_response)
            response = header + body

            new_socket.send(response.encode("gbk"))
        new_socket.close()
示例#14
0
 def  service_client(self,new_socket):
     '''为这个客户端返回数据'''
     #接受浏览器发送的请求,即http请求
     #GET /HTTP/1.1
     print("*" * 50)
     requet = new_socket.recv(1024).decode("utf-8")
     #print(requet)
     # 将request得到的内容按照行进行切割为列表
     request_lines = requet.splitlines()
     print(request_lines)
     #得到的request_lines[0]的请求网页名字
     #get,post,put,del
     ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
     if ret:
         files_name = ret.group(1)
         print(files_name)
     #判断请求的页面是否是.py结尾,如果是说明是动态资源
     #如果不是.py结尾,那么就认为是静态资源
     if not files_name.endswith(".py"):
     # - 准备发送给浏览器的数据。。。body,打开文件
     # response += "<h1>woshi ge shuage </>"
         try:
             f = open("D:/demo" + files_name, "rb")
         except:
             response = "HTTP/1.1 404 NOT FOUNT\r\n"
             response +="\r\n"
             response +="------file not fount-------"
             # 将response头发给浏览器
             new_socket.send(response.encode("utf-8"))
         else:
             html_contex = f.read()
             f.close()
             # 返回http格式的数据给浏览器
             # - 准备发送给浏览器的数据。。。header
             response = "HTTP/1.1 200 OK \r\n"
             response += "\r\n"
             # 将response头发给浏览器
             new_socket.send(response.encode("utf-8"))
             # 将responsebody发给浏览器
             new_socket.send(html_contex)
     else:
         #如果是以.py结尾,那么就认为是动态资源请求
         header = "HTTP/1.1 200 OK\r\n"
         header +="\r\n"
         '''if files_name=="/login.py":
             body = mini_frame.login()
         elif files_name=="/register.py":
             body = mini_frame.register()'''
         #将
         body = mini_frame.application(files_name)
         response = header + body
         new_socket.send(response.encode("utf-8"))
     # 关闭套接字
     new_socket.close()
示例#15
0
    def handle_client(self, client_socket):
        """为一个客户端服务"""
        # 1.接收浏览器发送的请求
        env = dict()
        recv_data = client_socket.recv(1024).decode("utf-8")
        # 按行分割,方便字典存储
        lines = recv_data.splitlines()
        for line in lines:
            ret = line.split(": ")
            if len(ret) == 2:
                (key, value) = line.split(": ")
                env[key] = value
        # print(env)
        # 2.获取请求的url \S+匹配GET POST PUT DEL等  \s匹配空白字符 (\S+)匹配url \s匹配空白字符
        ret = re.match(r"^\S+\s(\S+)\s", lines[0])
        if ret:
            url = ret.group(1)
        else:
            print(ret)
            print(lines)
        # 3.组装应答头和应答体
        # 3.1 如果浏览器请求的是动态资源
        if url.endswith(".py"):
            # 框架中的application返回body
            env['url'] = url
            response_body = mini_frame.application(env, self.set_response_header).encode("utf-8")
            # 拼接header
            response_header = "HTTP/1.1 %s\r\n" % self.status_code
            for i in self.headers:
                response_header += "%s:%s\r\n" % (i[0], i[1])
            response_header += "\r\n"
            response_header = response_header.encode("utf-8")
            print(response_header)

        # 3.2 如果浏览器请求的是静态资源,如html/css/js/png/gif等
        else:
            # 设置默认页面,如果url为/,则跳转到index.html
            if url == "/":
                url = "/index.html"
            # 组装
            try:  # 尝试打开文件
                with open(BASE_DIR + url, mode="rb") as f:  # 必须以rb的形式读取,因为有时传输的是图片
                    response_body = f.read()
            except Exception:  # 如果出现异常
                response_header = "HTTP/1.1 404 Error \r\n\r\n".encode("utf-8")
                response_body = "<h1>404 Page Not Found</h1>".encode("utf-8")
            else:  # 如果没有异常
                response_header = "HTTP/1.1 200 0K \r\n\r\n".encode("utf-8")
        # 拼接应答
        response = response_header + response_body
        # 4.返回应答
        client_socket.send(response)
        # 5.关闭套接字
        client_socket.close()
示例#16
0
    def response_dynamic(self, file, client_socket):
        temp_dict = dict()
        temp_dict['FILE_PATH'] = file
        body = mini_frame.application(temp_dict, self.start_response)  # 向应用框架mimi_frame传入函数start_response

        res_header = "HTTP/1.1 %s\r\n" % self.status
        for line in self.header_line:  # 每一个line都是元组
            res_header += '%s:%s\r\n' % (line[0], line[1])
        res_header += '\r\n'
        print("res_header:")
        print(res_header)

        client_socket.send(res_header.encode("utf-8"))  # 可以先回复头,在socket.close()之前再回复body
        client_socket.send(body.encode("utf-8"))
    def service_client(self, new_socket):
        """为这个客户端返回数据"""

        # 1.接收浏览器发送过来的请求, 即 http 请求
        # GET / HTTP/1.1
        # 。。。
        request = new_socket.recv(1024).decode("utf-8")

        request_lines = request.splitlines()

        ret = re.match(r"[^/]+(/[^ ]*)",request_lines[0])
        if ret:
            file_name = ret.group(1)
            if file_name == "/":
                file_name = "/index.html"

        # 2.返回 http 格式的数据给浏览器
        # 如果请求的资源不是以.py结尾,那么就认为是静态资源 (html,css,js,jpg,等)        
        # header
        if not file_name.endswith(".py"):
            try:
                f = open("../html" + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n" 
                response += "\r\n"
                response += "------file not found------"
                new_socket.send(response.encode("utf-8"))
            else:
                html_content = f.read()
                f.close()
                # header
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                # 分两次发送
                new_socket.send(response.encode("utf-8"))
                new_socket.send(html_content)
        else:
            # 如果是以.py结尾,那么就认为是动态资源请求
            header = "HTTP/1.1 200 OK\r\n"
            header += "\r\n"

            body = mini_frame.application(file_name)

            response = header + body
            # 发送 response 给浏览器
            new_socket.send(response.encode("utf-8"))

        # 关闭套接字
        new_socket.close()
    def service(self, client_socket):
        """对客户端服务的相应操作"""
        # 接收来自客户端的数据
        request_data = client_socket.recv(1024).decode("utf-8")
        request_lines = request_data.splitlines()
        # print(request_lines)

        # 用正则表达式匹配客户端访问的文件名
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
        # 如果正则表达式有匹配的结果,则用文件名接收此结果
        file_name = ""
        if ret:
            file_name = ret.group(1)
            if file_name == "/":
                file_name = "/index.html"

        # 根据文件名打开相应文件,将html的数据发送给客户端
        # 如果请求的资源不是以.py结尾,那么就认为是静态资源(html/css/js/png.jpg等)
        if not file_name.endswith(".py"):
            try:
                f = open("D:/python/3.web服务器/3.简单web服务器实现/html" + file_name,
                         "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "----file not found-----"
                client_socket.send(response.encode("utf-8"))
            else:
                # 读取文件数据
                file_content = f.read()
                # 给客户端响应
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                client_socket.send(response.encode("utf-8"))
                client_socket.send(file_content)
        # 如果是以.py结尾的那么就认为是动态资源的请求
        else:
            header = "HTTP/1.1 200 OK\r\n"
            header += "\r\n"

            # body = mini_frame.login()
            body = mini_frame.application(file_name)

            response = header + body
            # 发送response给浏览器
            client_socket.send(response.encode("utf-8"))

        # 关闭套接字
        client_socket.close()
示例#19
0
    def service_client(self, new_socket):
        request = new_socket.recv(1024).decode("utf-8")

        request_lines = request.splitlines()
        print("")
        print(">" * 20)
        print(request_lines)

        file_name = ""
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
        if ret:
            file_name = ret.group(1)
            if file_name == "/":
                file_name = "/index.html"

        if not file_name.endswith(".py"):
            try:
                f = open("./html" + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "-----file not found----"
                new_socket.send(response.encode("utf-8"))
            else:
                html_content = f.read()
                f.close()

                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"

                new_socket.send(response.encode("utf-8"))
                new_socket.send(html_content)

        else:
            header = "HTTP/1.1 200 OK\r\n"
            header += "\r\n"

            body = mini_frame.application(file_name)
            response = header + body
            new_socket.send(response.encode("utf-8"))

        new_socket.close()
示例#20
0
    def service_client(self, new_socket):
        request = new_socket.recv(1024).decode("utf-8")
        # print(request)
        request_lines = request.splitlines()
        print("")
        print(">" * 20)
        print(request_lines)

        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
        if ret:
            file_name = ret.group(1)
            # print("*" * 50, file_name)
            if file_name == "/":
                file_name = "/index.html"
        if not file_name.endswith(".py"):
            try:
                f = open("." + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "------file not found------"
                new_socket.send(response.encode("utf-8"))
            else:
                html_content = f.read()
                f.close()
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                new_socket.send(response.encode("utf-8"))
                new_socket.send(html_content)
        else:
            env = dict()
            env['PATH_INFO'] = file_name
            body = mini_frame.application(env, self.set_response_header)
            header = "HTTP/1.1 %s\r\n" % self.status
            for temp in self.headers:
                header += "%s:%s\r\n" % (temp[0], temp[1])
            header += "\r\n"
            response = header + body
            new_socket.send(response.encode("utf-8"))
        new_socket.close()
示例#21
0
 def service_client(self, new_socket):
     #接受http请求GET / HTTP/1.1
     request = new_socket.recv(1024).decode('utf-8')
     re_list = request.splitlines()[0]
     name = ''
     name += re.match(r'[^/]+(/[^ ]*)', re_list).group(1)
     if name == '/':
         name = '/index.html'
     print(request)
     #返回http数据
     #如果返回请求资源不是以.py结尾,那么就认为是静态资源
     if not name.endswith(".py"):
         response = 'HTTP/1.1 200 OK\r\n'
         response += '\r\n'
         try:
             f = open("./html" + name, 'rb')
         except:
             response = "HTTP/1.1 404 NOT FOUND\r\n"
             response += "\r\n"
             response += "------file not found-------"
         else:
             body = f.read()
             f.close()
             new_socket.send(response.encode('utf-8'))
             new_socket.send(body)
     else:
         env = dict()
         env["PATH_INFO"] = name
         body = mini_frame.application(env, self.set_response_header)
         response = "HTTP/1.1 %s\r\n" % self.status
         for temp in self.header:
             response += "%s:%s\r\n" % (temp[0], temp[1])
         response += "\r\n"
         response += body
         new_socket.send(response.encode("utf-8"))
     new_socket.close()
    def service_client(self, new_socket):
        "为一个客户端进行服务,为这个客户端返回数据"

        # 1. 接收浏览器发送过来的请求,即HTTP请求
        request_data = new_socket.recv(1024).decode("utf-8")
        # 将请求报文以行分隔为列表
        request_header_lines = request_data.splitlines()
        # 格式化打印出请求报文信息,换行打出
        for line in request_header_lines:
            print(line)

        # 提取出请求网页的名称,即/后面的内容
        # 先取出请求头的第一行
        request_line = request_header_lines[0]
        # 匹配出/之外的任何字符,就是从GET开始匹配,然后从后面的/之后视为一个分组
        # 匹配出出空格外的任何内容,即从第一个/开始匹配到空格结束
        # 请求头的第一行request_line:GET /index.html HTTP/1.1
        # 匹配结果:GET /index.html 我们提取出/及以后的内容
        # [^/]+表示匹配除了/以为的任何字符多次,/[^ ]*表示从/开始匹配任何字符,+匹配1次或多次,*匹配0次或多次
        file_name = re.match("[^/]+(/[^ ]*)", request_line).group(1)
        # 加入网页所在的系统路径,网页都是放在html文件夹中,此时html是放在上级目录,上上级使用../../html
        html_file_name = "../html" + file_name
        print("file name is ===>%s" % file_name)
        print('*' * 50)

        # 2. 返回http格式的数据给浏览器
        # 返回header(http协议内容)和body(浏览器要显示的内容)

        # 2.1 如果请求的资源不是以.py结尾,就认为是静态资源(比如html,css,js,png,jpg等)
        if not file_name.endswith('.py'):
            # 请求的网页也可能不存在,加入try语句
            try:
                f = open(html_file_name, 'rb')

            except:
                # 如果请求的页面不能打开,即不存在,返回以下信息
                response_header = "HTTP/1.1 404 not found\r\n"
                response_header += "\r\n"
                response_body = "====sorry ,file not found===="
                response = response_header + response_body
                new_socket.send(response.encode("utf-8"))

            else:
                # 页面存在,则返回相应的页面信息
                # 2.1.1 组织响应头信息(header),浏览器中换行使用\r\n
                response_header = "HTTP/1.1 200 OK\r\n"  # 200表示找到这个资源
                response_header += "\r\n"  # 用一个空的行与body进行隔开,作为换行符
                # 组织内容(body)
                # 返回一个本地已经编辑好的前端html页面
                response_body = f.read()
                f.close()
                # 2.1.2 组织响应报文,发送数据,由于已经不是单纯的字符串,不能使用拼接
                # 头和体信息单独发送,或者编码后一起发送
                response = response_header.encode("utf-8") + response_body
                new_socket.send(response)

        else:
            # 2.2 如果是以.py结尾,那么就认为是动态资源请求
            response_header = "HTTP/1.1 200 OK\r\n"
            response_header += "\r\n"
            # response_body = '哈哈哈,我是动态资源请求的结果 %s ' % time.ctime()
            # 调用mini_frame专门用于动态请求
            response_body = mini_frame.application(file_name)

            # 浏览器默认解析使用的是gbk,gbk可以显示更多的汉字,此处使用utf-8会乱码
            # 如果是html源码,meta标签里面的charset指定编码是utf-8就可以完美显示中文了
            response = response_header + response_body
            new_socket.send(response.encode("gbk"))

        # 3. 关闭客户端套接字
        new_socket.close()
示例#23
0
    def service_client(self, client):
        #接收流览器发过来的请求
        #GET / HTTP/1.1
        #.......
        request = client.recv(1024).decode('utf-8')

        request_lines = request.splitlines()
        #print(request.decode('utf-8'))

        print(request_lines)
        print(">>>" * 20)

        file_name = ""
        #如果发送数据空,则直接结束
        #正则匹配TCP头的参数
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
        #如果ret不为空,返回后缀
        if ret:
            file_name = ret.group(1)
            if file_name == "/":
                file_name = "/index.html"

        #返回浏览器请求的数据

        #打开客户请求的页面
        #非.py结尾的请求资源则认为是静态资源(html\css\js等)
        if not file_name.endswith(".py"):
            try:
                f = open("./html" + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "------------not found------------"
                client.send(response.encode('utf-8'))
            else:
                html_content = f.read()
                f.close()
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"

                client.send(response.encode('utf-8'))
                client.send(html_content)
        else:
            #如果是以.py结尾的请求资源,则认为是动态资源

            env = dict()

            body = mini_frame.application(env, self.set_response_header)

            header = "HTTP.1.1 %s\r\n" % self.status

            self.header += [('server', 'mini-server v1.0')]

            for temp in self.header:
                header += "%s:%s\r\n" % (temp[0], temp[1])

            header += "\r\n"

            response = header + body

            client.send(response.encode('utf-8'))

        #关闭套接字
        client.close()
示例#24
0
    def service_client(self, tcp_client_socket):
        """为客户端服务"""

        # 1. 接收浏览器发送过来的 http 请求
        # GET /index.html HTTP/1.1
        # ......
        # 
        # 请求数据内容,对数据内容进行解码
        request = tcp_client_socket.recv(1024).decode("utf-8")
        print(request)

        try:
            # 对接收到的请求协议字符串进行按行切割
            # 返回的是由每一行组成的一个列表
            request_lines = request.splitlines()

            # 第一行就是http请求头,其中有浏览器需要访问的文件名
            ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])

            # 获取文件名 /index.html
            if ret:
                file_name = ret.group(1)
                if file_name == "/":
                    file_name = "/index.html"
            else:
                pass

        except IndexError:
            file_name = "/index.html"

        # 2.返回http格式的数据给浏览器
        # 如果请求的资源不是以.py为结尾,那么就认为是静态资源(html/css/js/png,jpg等)
        if not file_name.endswith(".py"):
            try:
                f = open("./html" + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "------file not found------"
                tcp_client_socket.send(response.encode("utf-8"))
            else:
                html_content = f.read()
                f.close()
                # 2.1 发给浏览器的数据----header
                # 注意末尾换行一定要加上\r\n 表示换行                   
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"  # 在协议头和 请求的数据之间有一个空行

                # 2.2 发给浏览器的数据----body 
                # response += "<h1>YangHang love ZhangZifan</h1>"
                
                # 发送回应头
                tcp_client_socket.send(response.encode("utf-8"))
                # 发送客户端请求的内容
                tcp_client_socket.send(html_content)
        else:
            # 如果是以.py结尾,那么就认为是动态资源请求
           
            # body = "hhhh"
            # if file_name == "/login.py":
            #     body = mini_frame.login()
            # 实现解耦, 在简单框架内进行逻辑处理
            
            #WSGI协议
            env = dict()
            body = mini_frame.application(env, self.set_response_header)
            
            header = "HTTP/1.1 %s\r\n" % self.status
            # 遍历响应头的元组
            for temp in self.headers:
                header +="%s:%s\r\n" % (temp[0], temp[1])
            
            header += "\r\n"

            response = header + body
            tcp_client_socket.send(response.encode("utf-8"))
           
        # 关闭服务套接字
        tcp_client_socket.close()
    def service_client(self, new_client_socket):
        # 1.接收浏览器请求
        request = new_client_socket.recv(1024).decode("utf-8")
        print(request)

        # 获取请求路径
        # GET /index.html HTTP/1.1
        # 提取 /index.html
        request_lines = request.splitlines()
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])

        file_name = ""
        if ret:
            file_name = ret.group(1)
            if file_name == "/":
                file_name = "/index.html"

        # 2. 返回http格式的数据,给浏览器
        if not file_name.endswith(".py"):
            # 2.1 如果请求的资源不是以.py结尾的,就加载静态资源(html/css/js/png/jpg等)
            try:
                f = open("./html" + file_name, "rb")

            except (FileNotFoundError, NotADirectoryError):
                # 文件或目录不存在,响应404
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "file not found!"
                new_client_socket.send(response.encode("utf-8"))

            else:
                # 2.返回给浏览器的数据
                # Header
                response = "HTTP/1.1 200 OK\r\n"  # 这里必须有换行\r\n
                response += "\r\n"  # 这里必须有换行\r\n,区分Header和body

                # Body
                content = f.read()
                f.close()

                # 发送
                new_client_socket.send(response.encode("utf-8"))
                new_client_socket.send(content)

        else:
            # 2.2 如果路径以.py结尾,则加载动态资源(动态解析)
            header = "HTTP/1.1 200 OK\r\n"
            header += "\r\n"

            # body = "Hello,Python!%s" % time.ctime()
            # body = ""
            # if file_name == "/login.py":
            #     body = mini_frame.login()
            # elif file_name == "/register.py":
            #     body = mini_frame.register()
            # elif file_name == "/other.py":
            #     body = mini_frame.other()
            # else:
            #     body = "page not found!404!"

            body = mini_frame.application(file_name)

            response = header + body
            new_client_socket.send(response.encode("utf-8"))

        # 3.关闭连接
        new_client_socket.close()  # 这是关闭子进程的new_client_socket的,因为子进程会复制父进程
示例#26
0
    def handler_req(self, client_socket):
        """
        对客户端请求进行处理,返回需求数据
        :param client_socket: 临时套接字,与一个客户端通信
        :return: 返回请求资源,未找到返回404
        """

        # 长连接方式:接收请求数据
        while True:
            try:
                recv_data = client_socket.recv(1024).decode("utf-8")
                print(recv_data)
            except Exception as ret:
                print(">" * 50, ret)
                client_socket.close()
                return

            # 判断浏览器是否关闭
            if not recv_data:
                client_socket.close()
                return

            # 从请求数据内提取请求文件名
            request_lines = recv_data.splitlines()
            ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])

            file_name = ""
            if ret:
                file_name = ret.group(1)
                if file_name == "/":
                    file_name = "/index.html"

            if not file_name.endswith('.py'):
                # 根据请求文件名打开文件读取,返回文件内容给客户端
                try:
                    f = open("./html" + file_name, "rb")
                except FileNotFoundError:
                    response_body = "------file not found------"

                    response_header = "HTTP/1.1 404 not found\r\n"
                    response_header += "Content-Length: %d\r\n" % (
                        len(response_body))
                    response_header += "\r\n"

                    response = response_header + response_body
                    client_socket.send(response.encode("utf-8"))
                else:
                    html_content = f.read()
                    f.close()
                    response_body = html_content

                    response_header = "HTTP/1.1 200 OK\r\n"
                    response_header += "Content-Length: %d\r\n" % (
                        len(response_body))
                    response_header += "\r\n"

                    response = response_header.encode("utf-8") + response_body
                    client_socket.send(response)
            else:
                env = dict()
                response_body = mini_frame.application(
                    env, self.set_response_header)

                response_header = "HTTP/1.1 %s\r\n" % self.status
                response_header += "Content-Length: %d\r\n" % (len(
                    response_body.encode("utf-8")))

                for temp in self.headers:
                    response_header += "%s:%s\r\n" % (temp[0], temp[1])

                response_header += "\r\n"

                response = response_header + response_body
                client_socket.send(response.encode("utf-8"))

        client_socket.close()
示例#27
0
    def service_client(self, new_socket):
        """为这个客户端返回数据"""

        # 1. 接收浏览器发送过来的请求 ,即http请求
        # GET / HTTP/1.1
        # .....
        request = new_socket.recv(1024).decode("utf-8")
        # print(">>>"*50)
        # print(request)

        request_lines = request.splitlines()
        print("")
        print(">" * 20)
        print(request_lines)

        # GET /index.html HTTP/1.1
        # get post put del
        file_name = ""
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
        if ret:
            file_name = ret.group(1)
            # print("*"*50, file_name)
            if file_name == "/":
                file_name = "/index.html"

        # 2. 返回http格式的数据,给浏览器
        # 2.1 如果请求的资源不是以.py结尾,那么就认为是静态资源(html/css/js/png,jpg等)
        if not file_name.endswith(".py"):
            try:
                f = open("./html" + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "------file not found-----"
                new_socket.send(response.encode("utf-8"))
            else:
                html_content = f.read()
                f.close()
                # 2.1 准备发送给浏览器的数据---header
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                # 2.2 准备发送给浏览器的数据---boy
                # response += "hahahhah"

                # 将response header发送给浏览器
                new_socket.send(response.encode("utf-8"))
                # 将response body发送给浏览器
                new_socket.send(html_content)
        else:
            # 2.2 如果是以.py结尾,那么就认为是动态资源的请求

            env = dict()  # 这个字典中存放的是web服务器要传递给 web框架的数据信息
            env['PATH_INFO'] = file_name
            # {"PATH_INFO": "/index.py"}
            # body = "hahahah %s " % time.ctime()
            # if file_name == "/login.py":
            #     body = mini_frame.login()
            # elif file_name == "/register.py":
            #     body = mini_frame.register()
            # 相对上面注释具有解耦功能
            body = mini_frame.application(env,
                                          self.set_response_header)  # wsgi协议格式

            header = "HTTP/1.1 %s\r\n" % self.status

            for temp in self.headers:
                header += "%s:%s\r\n" % (temp[0], temp[1])

            header += "\r\n"

            response = header + body
            # 发送response给浏览器
            new_socket.send(response.encode("utf-8"))

        # 关闭套接
        new_socket.close()
示例#28
0
    def service_client(self, new_socket):
        """为这个客户端返回数据"""

        # 1. 接收浏览器发送过来的请求 ,即http请求  
        # GET / HTTP/1.1
        # .....
        request = new_socket.recv(1024).decode("utf-8")
        # print(">>>"*50)
        # print(request)

        request_lines = request.splitlines()
        print("")
        print(">"*20)
        print('request_lines:', request_lines)
        """打印结果:
        request_lines: 
            ['GET /mini_frame.py HTTP/1.1', 
            'Host: 127.0.0.1:7892', 
            'Connection: keep-alive', 
            'Cache-Control: max-age=0', 
            'Upgrade-Insecure-Requests: 1', 
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) \
            Chrome/69.0.3497.100 Safari/537.36', 
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 
            'Accept-Encoding: gzip, deflate, br', 
            'Accept-Language: zh-CN,zh;q=0.9', 
            'Cookie: csrftoken=XeNEgFEg7BlsE8hp9SKGbVXs0I4j5PY2N10rupEv3B5jMLpQFVs8f4MwCSmXX0mq; \
            sessionid=5bp4uzk26v87sugzrg7jhup8bhxuyj4c', 
            '']
        """

        # request_lines[0] = 'GET /mini_frame.py HTTP/1.1'
        # get post put del
        file_name = ""
        # 正则解释:是匹配以非‘/’的字符+非多个空格的字符
        ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
        print('ret:', ret)
        # ret: <_sre.SRE_Match object; span=(0, 18), match='GET /mini_frame.py'>
        if ret:
            file_name = ret.group(1)
            print("*"*50, file_name) # *** /mini_frame.py

            if file_name == "/":
                file_name = "/index.html"

        # 2. 返回http格式的数据,给浏览器
        # 2.1 如果请求的资源不是以.py结尾,那么就认为是静态资源(html/css/js/png,jpg等)
        if not file_name.endswith(".py"):
            try:
                f = open("./html" + file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "------file not found-----"
                new_socket.send(response.encode("utf-8"))
            else:
                html_content = f.read()
                # print('--------html_content---------')
                # print('html_content = f.read():', html_content) # 静态文件读取html/css/image等文件
                # print('--------html_content---------')
                # html_content返回浏览器打印结果:html-->css-->image依次转换访问返回给浏览器
                """
				>>>>>>>>>>>>>>>>>>>>
request_lines: ['GET / HTTP/1.1', 'Host: 127.0.0.1:7892', 'Connection: keep-alive', 'Cache-Control: max-age=0', 'Upgrade-Insecure-Requests: 1', 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Accept-Encoding: gzip, deflate, br', 'Accept-Language: zh-CN,zh;q=0.9', 'Cookie: csrftoken=XeNEgFEg7BlsE8hp9SKGbVXs0I4j5PY2N10rupEv3B5jMLpQFVs8f4MwCSmXX0mq; sessionid=5bp4uzk26v87sugzrg7jhup8bhxuyj4c', '']
ret: <_sre.SRE_Match object; span=(0, 5), match='GET /'>
************************************************** /
--------html_content---------
html_content = f.read(): b'<?xml version="1.0" encoding="iso-8859-1"?>\n<!DOCTYPE html\n    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/index.qdoc -->\n<head>\n  <title>Qt 4.3: Qtopia Core Reference Documentation</title>\n  <link href="classic.css" rel="stylesheet" type="text/css" />\n</head>\n<body>\n<table border="0" cellpadding="0" cellspacing="0" width="100%">\n<tr>\n<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>\n<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>\n<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Qtopia Core Reference Documentation<br /><small>Qt for Embedded Linux</small></h1>\n<a name="qt-reference-documentation"></a>        <table cellpadding="2" cellspacing="1" border="0" width="100%" bgcolor="#e5e5e5">\n        <tr>\n        <th bgcolor="#a2c511" width="33%">\n        Getting Started\n        </th>\n        <th bgcolor="#a2c511" width="33%">\n        General\n        </th>\n        <th bgcolor="#a2c511" width="33%">\n        Developer Resources\n        </th>\n        </tr>\n        <tr>\n        <td valign="top">\n        <ul>\n        <li><strong><a href="qt4-3-intro.html">What\'s New in Qt 4.3</a></strong></li>\n        <li><a href="how-to-learn-qt.html">How to Learn Qt</a></li>\n        <li><a href="installation.html">Installation</a></li>\n        <li><a href="tutorial.html">Tutorial</a> and <a href="examples.html">Examples</a></li>\n        <li><a href="porting4.html">Porting from Qt 3 to Qt 4</a></li>\n        </ul>\n        </td>\n        <td valign="top">\n        <ul>\n        <li><a href="aboutqt.html">About Qt</a></li>\n        <li><a href="trolltech.html">About Trolltech</a></li>\n        <li><a href="commercialeditions.html">Commercial Edition</a></li>\n        <li><a href="opensourceedition.html">Open Source Edition</a></li>\n        <li><a href="http://www.trolltech.com/developer/faqs/">Frequently Asked Questions</a></li>\n        </ul>\n        </td>\n        <td valign="top">\n        <ul>\n        <li><a href="http://lists.trolltech.com">Mailing Lists</a></li>\n        <li><a href="http://www.trolltech.com/developer/community/">Qt Community Web Sites</a></li>\n        <li><a href="http://doc.trolltech.com/qq/">Qt Quarterly</a></li>\n        <li><a href="bughowto.html">How to Report a Bug</a></li>\n        <li><a href="http://www.trolltech.com/developer/">Other Online Resources</a></li>\n        </ul>\n        </td>\n        </tr>\n        <tr>\n        <th bgcolor="#a2c511">\n        API Reference\n        </th>\n        <th bgcolor="#a2c511">\n        Core Features\n        </th>\n        <th bgcolor="#a2c511">\n        Key Technologies\n        </th>\n        </tr>\n        <tr>\n        <td valign="top">\n        <ul>\n        <li><a href="classes.html">All Classes</a></li>\n        <li><a href="mainclasses.html">Main Classes</a></li>\n        <li><a href="groups.html">Grouped Classes</a></li>\n        <li><a href="annotated.html">Annotated Classes</a></li>\n        <li><a href="modules.html">Qt Classes by Module</a></li>\n        <li><a href="hierarchy.html">Inheritance Hierarchy</a></li>\n        <li><a href="functions.html">All Functions</a></li>\n        <li><a href="qtopiacore.html">Qtopia Core</a></li>\n        <li><a href="overviews.html">All Overviews and HOWTOs</a></li>\n        <li><a href="gallery.html">Qt Widget Gallery</a></li>\n        <li><a href="http://doc.trolltech.com/extras/qt43-class-chart.pdf">Class Chart</a></li>\n        </ul>\n        </td>\n        <td valign="top">\n        <ul>\n        <li><a href="signalsandslots.html">Signals and Slots</a></li>\n        <li><a href="object.html">Object Model</a></li>\n        <li><a href="layout.html">Layout Management</a></li>\n        <li><a href="paintsystem.html">Paint System</a></li>\n        <li><a href="graphicsview.html">Graphics View</a></li>\n        <li><a href="accessible.html">Accessibility</a></li>\n        <li><a href="containers.html">Tool and Container Classes</a></li>\n        <li><a href="i18n.html">Internationalization</a></li>\n        <li><a href="plugins-howto.html">Plugin System</a></li>\n        <li><a href="intro-to-dbus.html">Inter-process Communication</a></li>\n        <li><a href="qtestlib-manual.html">Unit Testing Framework</a></li>\n        </ul>\n        </td>\n        <td valign="top">\n        <ul>\n        <li><a href="threads.html">Multithreaded Programming</a></li>\n        <li><a href="qt4-mainwindow.html">Main Window Architecture</a></li>\n        <li><a href="richtext.html">Rich Text Processing</a></li>\n        <li><a href="model-view-programming.html">Model/View Programming</a></li>\n        <li><a href="stylesheet.html">Style Sheets</a></li>\n        <li><a href="qtnetwork.html">Network Module</a></li>\n        <li><a href="qtopengl.html">OpenGL Module</a></li>\n        <li><a href="qtsql.html">SQL Module</a></li>\n        <li><a href="qtsvg.html">SVG Module</a></li>\n        <li><a href="qtxml.html">XML Module</a></li>\n        <li><a href="qtscript.html">Script Module</a></li>\n        <li><a href="activeqt.html">ActiveQt Framework</a></li>\n        </ul>\n        </td>\n        </tr>\n        <tr>\n        <th bgcolor="#a2c511">\n        Add-ons &amp; Services\n        </th>\n        <th bgcolor="#a2c511">\n        Tools\n        </th>\n        <th bgcolor="#a2c511">\n        Licenses &amp; Credits\n        </th>\n        </tr>\n        <tr>\n        <td valign="top">\n        <ul>\n        <li><a href="http://www.trolltech.com/products/qt/addon/solutions/">Qt Solutions</a></li>\n        <li><a href="http://www.trolltech.com/products/qt/3rdparty/">Partner Add-ons</a></li>\n        <li><a href="http://qt-apps.org">Third-Party Qt Components (qt-apps.org)</a></li>\n        <li><a href="http://www.trolltech.com/support/">Support</a></li>\n        <li><a href="http://www.trolltech.com/support/training/">Training</a></li>\n        </ul>\n        </td>\n        <td valign="top">\n        <ul>\n        <li><a href="designer-manual.html">Qt Designer</a></li>\n        <li><a href="assistant-manual.html">Qt Assistant</a></li>\n        <li><a href="linguist-manual.html">Qt Linguist</a></li>\n        <li><a href="qmake-manual.html">qmake</a></li>\n        <li><a href="qttools.html">All Tools</a></li>\n        </ul>\n        </td>\n        <td valign="top">\n        <ul>\n        <li><a href="gpl.html">GNU General Public License</a></li>\n        <li><a href="3rdparty.html">Third-Party Licenses Used in Qt</a></li>\n        <li><a href="licenses.html">Other Licenses Used in Qt</a></li>\n        <li><a href="trademarks.html">Trademark Information</a></li>\n        <li><a href="credits.html">Credits</a></li>\n        </ul>\n        </td>\n        </tr>\n        </table>\n    <p /><address><hr /><div align="center">\n<table width="100%" cellspacing="0" border="0"><tr class="address">\n<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>\n<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>\n<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>\n</tr></table></div></address></body>\n</html>\n'
--------html_content---------
>>>>>>>>>>>>>>>>>>>>
request_lines: ['GET /classic.css HTTP/1.1', 'Host: 127.0.0.1:7892', 'Connection: keep-alive', 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', 'Accept: text/css,*/*;q=0.1', 'Referer: http://127.0.0.1:7892/', 'Accept-Encoding: gzip, deflate, br', 'Accept-Language: zh-CN,zh;q=0.9', 'Cookie: csrftoken=XeNEgFEg7BlsE8hp9SKGbVXs0I4j5PY2N10rupEv3B5jMLpQFVs8f4MwCSmXX0mq; sessionid=5bp4uzk26v87sugzrg7jhup8bhxuyj4c', '']
ret: <_sre.SRE_Match object; span=(0, 16), match='GET /classic.css'>
************************************************** /classic.css
--------html_content---------
html_content = f.read(): b'h3.fn,span.fn\n{\n  margin-left: 1cm;\n  text-indent: -1cm;\n}\n\na:link\n{\n  color: #004faf;\n  text-decoration: none\n}\n\na:visited\n{\n  color: #672967;\n  text-decoration: none\n}\n\ntd.postheader\n{\n  font-family: sans-serif\n}\n\ntr.address\n{\n  font-family: sans-serif\n}\n\nbody\n{\n  background: #ffffff;\n  color: black\n}\n\ntable tr.odd {\n  background: #f0f0f0;\n  color: black;\n}\n\ntable tr.even {\n  background: #e4e4e4;\n  color: black;\n}\n\ntable.annotated th {\n  padding: 3px;\n  text-align: left\n}\n\ntable.annotated td {\n  padding: 3px;\n}\n\ntable tr pre\n{\n  padding-top: none;\n  padding-bottom: none;\n  padding-left: none;\n  padding-right: none;\n  border: none;\n  background: none\n}\n\ntr.qt-style\n{\n  background: #a2c511;\n  color: black\n}\n\nbody pre\n{\n  padding: 0.2em;\n  border: #e7e7e7 1px solid;\n  background: #f1f1f1;\n  color: black\n}\n\nspan.preprocessor, span.preprocessor a\n{\n  color: darkblue;\n}\n\nspan.comment\n{\n  color: darkred;\n  font-style: italic\n}\n\nspan.string,span.char\n{\n  color: darkgreen;\n}\n\n.subtitle\n{\n    font-size: 0.8em\n}\n\n.small-subtitle\n{\n    font-size: 0.65em\n}\n'
--------html_content---------

>>>>>>>>>>>>>>>>>>>>
request_lines: ['GET /images/qt-logo.png HTTP/1.1', 'Host: 127.0.0.1:7892', 'Connection: keep-alive', 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', 'Accept: image/webp,image/apng,image/*,*/*;q=0.8', 'Referer: http://127.0.0.1:7892/', 'Accept-Encoding: gzip, deflate, br', 'Accept-Language: zh-CN,zh;q=0.9', 'Cookie: csrftoken=XeNEgFEg7BlsE8hp9SKGbVXs0I4j5PY2N10rupEv3B5jMLpQFVs8f4MwCSmXX0mq; sessionid=5bp4uzk26v87sugzrg7jhup8bhxuyj4c', '']
ret: <_sre.SRE_Match object; span=(0, 23), match='GET /images/qt-logo.png'>
************************************************** /images/qt-logo.png
--------html_content---------
html_content = f.read(): b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00 \x08\x03\x00\x00\x00D\xa4\x8a\xc6\x00\x00\x00\xbaPLTE \x1b\x1f \x1c\x1f\xa7\xd09#  \xa5\xcd9\xa7\xcf9(&!%! aq+KU\'R](#\x1f n\x83.~\x981\x82\x9e2\x91\xb24\x9b\xc07\xa2\xc98*(!.."h|-01"\x98\xbb6:=$t\x8c/@G%Ta)\x83\xa02\x8e\xae4Yg*\x95\xb85]m+EL&\xa0\xc67\xa1\xc8835#NX(q\x87.r\x88/bt,\x9a\xbf6Zh*\x87\xa43\x8a\xa93ar+\xa3\xca8\x8f\xaf4x\x900o\x85.{\x950\x97\xbb6|\x961IR\'\x80\x9b1\x9d\xc27k\x7f-69#,+!\'$ 68#Ub)cu,h\xeb\x00{\x00\x00\x01\xadIDATx^\x95\x93\xd7\xae\xdb@\x0cD=\xe46\xf5.\xd7^no\xe9\xfd\xff\x7f+\xcb\xc0\x16\xd6y\x88\x91\x91\xb0\x101\x87"!\x8cF\xea\x8a\xfe\r\x14\xc5\x15\xe0S~\t\x10\x91\xd2r\x9c\xb5\xabB\x80\xaa\xc5\xc4-\x97\xb7\xaf\xc5\xe5\x0e\x83\x9dE\rF\xfe\xb2]<W\xfao\x80V3\x03$I\x02\x9b`\xf4m}\xa7/\x01z\x99\x02\xd6\x13\xd6B\x84h\xa5C\x80VS\xdf\x8d&>fY\xea\'\x81\x11\x95!P\xcd\xbco\xe3\x85"/\xf5\x10Y0\\\x00Pf\x00\xeb*\x92b[\xe8\xcf?\x19h\x1e\xf5\x00T\x11,\xe2\xad\x12\xed\xdc\xfe\x03\x95\x1bX,\x87\x11\xb4h\x12t\x0b\xe9\xd7\x95\xb3\xe8\xdeQn\x18?\xde\x0f\xc0$I\x10\xab?\xfe\x93\x050\xeew\x1b\xe0{Ng\xe0\t\t\xde\xa4\x92~F3Q\xda\x81qs\x06\xd4\x12\x89\xcd\xe8\xec\xd7\x13\xad\xe8\xc8@{\x06\xf4=X\x80\x9d\xf8\xcc\xa6U\x9a\xde \x80>\x8d\xf8\x05FJ[g\x99\xc1\x80i+ZCF\xd0\txeF\xa4\x8a=\x18\xa6\x96#-\xa7@\xfd0,Yt`_f\x9d\xdf/\x15\xa2>\x18\xc6\xb8\xd7C\xe4\xe2\x11\xf0\xb1\xa4l\xec\xf7K\r\x18r\xbb\xe0S\xcfk\x80g%\xf5\x92\x03y\x073\xf6/\x14\xe4\xc1I\xcb&\xdf\x12iU\x1e\xa4@\xfc\x1c\xe4A\x97\x11\x98a6\xee\x98\xae\xa7\x86\xbdP\x7fy\x0e\x03\xb3\x8a\x18\x8c\x93\xe4\xc1\xe3\x19\x85\x91+o%\'\x0ci\xc6>\xae\x19\x87>\x04\x94V\x8f\xf7_\x8d\xb4\xd6cWTi-~\x00x\x91\xea\xe77m{3\xbf\x93*\x17?\x04Dt\x92\x14\x14\x866\x94\xbe\xfa\xf3\xfe/\xf0\x1b:\xb7\x1f\xde\xf9+;+\x00\x00\x00\x00IEND\xaeB`\x82'
--------html_content---------

>>>>>>>>>>>>>>>>>>>>
request_lines: ['GET /images/trolltech-logo.png HTTP/1.1', 'Host: 127.0.0.1:7892', 'Connection: keep-alive', 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', 'Accept: image/webp,image/apng,image/*,*/*;q=0.8', 'Referer: http://127.0.0.1:7892/', 'Accept-Encoding: gzip, deflate, br', 'Accept-Language: zh-CN,zh;q=0.9', 'Cookie: csrftoken=XeNEgFEg7BlsE8hp9SKGbVXs0I4j5PY2N10rupEv3B5jMLpQFVs8f4MwCSmXX0mq; sessionid=5bp4uzk26v87sugzrg7jhup8bhxuyj4c', '']
ret: <_sre.SRE_Match object; span=(0, 30), match='GET /images/trolltech-logo.png'>
************************************************** /images/trolltech-logo.png
--------html_content---------
html_content = f.read(): b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xcb\x00\x00\x00 \x08\x03\x00\x00\x00\x8b\xa6|@\x00\x00\x00oPLTE\xa5\xcd8\xfe\xfe\xfe\xa4\xa6\xa9\xb5\xb7\xb9\xd7\xd7\xd9\xef\xf0\xf0\x9e\xa0\xa3\xe6\xe6\xe7\xbd\xbf\xc1\xbf\xc0\xc2\xc5\xc6\xc8\xce\xcf\xd0\xcf\xd0\xd2\x9c\x9e\xa1\xde\xdf\xe0\x9a\x9c\x9f\xef\xef\xf0\xad\xaf\xb1\xf5\xf9\xe9\xaf\xb0\xb3\xac\xd1G\xd9\xea\xab\xe6\xf1\xc8\xde\xec\xb5\xe0\xee\xbc\xca\xe2\x8a\xb4\xd5Y\xff\xff\xff\xbb\xd9h\xec\xf5\xd6\xa9\xcf@\xe3\xef\xc2\xbf\xdbq\xc4\xde}\xf0\xf6\xde\xce\xe4\x92\xd3\xe6\x9dV\xbc3\xac\x00\x00\x054IDATx^\xddX\xd7v\xe3:\x0c\x14X\xd5\xbb{z\xf6\xff\xbf\xf1\x02\xac\x92)\xdf\xe8$/9\x19\xd9Z\x92\xa2\x80\x9d3\x00\x01\'\xbb}\x0f\xa7,\x9b\x9fo\xbf\x0b\xdf\xe5r@.\x1f\x7f\x84\xcb\xe9\xe5/\xe9\xf2\xfb\xb8\xfc\xfa|\x01\x84\xc4\xef\x1e.m\xab\xf1j\xb5v\x03\xba\xc3\rtX\xd7rm\x08\x8eG\x08\xba\xc0y\x0c\x0f\xedK\xeb\xbd=\xae\xc8\xf0\x98\xcc[\x1f\xce\x91y(q\x10\xd7\xb5\\\xf2\x90m]\xe4<\xe7e\xad%\xa4.\xc8\x81n%\xed\xec%d\x8c1\xa5\xf0\xeb\xee\x88\x86#\x97<\xae\x0c\\D60\x1e\xe6\xa71\xe8r\x9c\xaf\xe7\xf0L2\x84^q)\xd1B\xed\'\xb9b\x04\xeb\xcd\xfa\xa5\xed\xc2\xfa\xf5K"R\xd1\xe5\xa0\x1a\x076T\x80K\x93b\x83N\x1d\xc8\xba\xac\xea\xac\xe9\xf0\x8a\xa0\xe9$o\xc0\xdd\xc4\x80\x15\x12<\x95\xf7,\xcb\x0e\xe0t\x01\x9c\xbd\x1cFo\x98\xe1\xd6~\xcd\x05mT~\xc2\xbb\xae\x89\xeeh\xd2\xb0\x16\xb9(\xe7\xd7.y. +\x16\x98\xd0vn\xb8t+\x17P\xa2\x19t\xa0\x91\x8b\xc8\xd8\x80\x17\x02\r*f\x91\x83\xe5\xc2\xf8\xc4\xa7\x895\xf8\xa8p\\\x0c\x959p\x19/\x19\x91q\x0f\xe5\x80\\\xf4=\x97&p)\x18\x82\xbc\xa9\xe0j ]\x1a\xe7z0\xdf\xd6\x8b\xc2\r\x13\xc6\x8b\xb2,8\n$\x0c\x97\xe6\x9e\x8bq\xd0\x8a\xb2j3\x89\x00\t-\xa3Mf(%\xc5\x18Nk\xa0\xb9\x98\xc8\x97\xd5\x15>\x91\xca\xf55\xe4\x0bE\x1c\x0e\\\x98I\xa6\x12]\xba\x05\x170\xf6\xa4\xe4h\xaf\xb6\x8eHo\x81\xaeyOS\xfbuT\xa6F)\xc5E\x0f\x04J\x1cI.\x88\x8b^; \xe1e)\xea\xd2\xd7\x17M\\ \xee!\x7f\xc2Y\x1d\x90Le\x9e\xbd\xceH\xe5\x19\x16\xb5\x12N\xb44\xc6\x18\xd3\x0f\xb9\xa4\xb6\t6\xc68\xdc\xed\x91&0*\x19\xd7\x01\x87\xa9.\x85\rb\x90\xa2\x95\x9eK\x8f\\\x06\x99\xf8s\x81\xa2\xac\xb3\xf1\r\xa9\x1c\xd7\xf5\x05p\x94\xfd3.\xe5V\xbe\xec\xe0\x82{r4\x90\xbc\xc8Dr\x12K\x1etI\x1d\xactYs\t\'J\xed\x9d\x9d_\xec\x7f;\xd6\x17Op|\x90/\xc5\x03.\xcd\x8a\x8bj\xf2\xe8:\x86I\x95P\xd9\xca\x97x\xb8$\xba\xa4\xfe\xa0\xc2q\t8\xb8P8%u\xff\x8c\xc2\x9cl\x8c\xa9]\xba\xe4w\xba\xb0FqHe\xe1\xf2\x96@N{u\x81-\x7f \'7\x1e\x9f\xb2\xec\x02\x8e\x8b\xeb\xc7\xc2\xb2\x8f1\xbd+\xc6\xd8*_\x92\x18\xb3.\xddZ\xaaK\xe2 \xd5\x85m\xe4\x0b@\xcf\x1b\x97\x9b\x1f3\t`q\x8a}2\xc9\xf5\x06>\xc6\xfa]\xb9\xdf\xdcs1g\x1a\x98\x9b\x0f1\xb2\x94\xe6K\xd7\xb1\xd6\x9e\x87@7\xca\xfdT\x974\xf7\x0b\x81(\xa8\xf2L=y8\xce\xa4E\x88\xb1\x17?\xfe\x97eO\xe3\x0f\xce1J\xf3\t\xc1\xe9S\x02-\xc5\x10Ks_\x85RD\x9f-]\x923\x19\xa1l\xb5beoO\xe4E\x9f\xff|\xb9|\x8e\x9e\x97\xe3"\xd9>]\xd2|\xb1\xe8\x08&\x02*\x1b\n\x9b1\x86P\x8bn\x00\xbf_\x9dcy|chI\xf7;]\x80\x90rI\xf3eW}Q\x8a9\xa8\xc2p\xe9\xfe\x8f\x0b[\x02y\xed8\xc7\x14/\x0bN\x1ar\x8a\\\x97/\xe7\r\xfb\x17\xe4b\xf3\xe5\xdb\xf5e\xa2&Y\xd0M;]\x98\xe7\x92\xd6\x17\xa1\xfbp\xe5\xfb\xea\x8b\x12\x00 \x98=\x90\t\xe35\x9ec\x11\xf1\x1c\x03\xb6;\xf7\xd5v}\x89K1{\xd3\xfa\xa2\xbf>\xc7\xd4}\x8c\t_\\\x98\x8e\x02\x8c\x89\xfd#V\xd0\xc3-\x89\xb1\x9f\xd4\x17\xcd:\xb2\x94B\xee<\x93U\xda\x8f\xd9\xc0Q\xaa\x08\xc5\x9e\xfa\xfd5\xe0\xddWP\x18\xf6\xeb\xf2E}\x19\\4|\xbb\x87I\xfb1\'\x8cb\xad\x8b&\xdfZF\x00\x12\xa4\x10\xfbQ\xee7[u\x9f\xf5\xb0\xabVn\xf70I\x8c9Y\xbd/\xa0v\xe5m\x8c.h\xe9J\xfcp\x94\xe4\xfe\xee\xba\xbfq\x00kf\xfaY\xd8\xd5\x8f\xed\xca};\x14\xca\xfb\xb6\xbf\xbd\x9e\x16\xca\xc0y\x8eq\x07l_oIg\xcf:_\x96\xb9\x1f{sU\xacZ~\xd8\x1bc\xfd\xf6\xef\x97(\x8cq\x06\x14e\xd9\xf50\x1a\xc3\x00\x1f\x17\xa2\xf2\t\xb7\xf8[LC\x84s\x05\x11\xd6v\xb7\x11c\xebM\xd0\x9b\xa2\xc8[\xf7\nH\xc1k\xd8\xd9\x8f\xe9\x071\x16\x85\xb1F?\x88Lv\xbd\x9c^\x9f_\x0f\xef\xc4d\xbeP\xe2\x87|\xc9KB\x81W\r6\xf7\'\x9c\xdb\xb5\xb2\x7f\x94/C\x19PI\xff\x0b\x90\xa4\xe1\x95hu[\x17\x83j&\xb9\xdd\'w\xfbz\xcbX\xa0\xc8\x92U\xe6\xfd%#\xcc\xb3\xfb\xf7\x00\x10\xb8\xa00\x11\x93$.\xae\xd3\xe8\xe8\xae\xda\x07\\\x08fO\xac+\xa093s\xe6\xad2\xf1 \xc6\xeaD\x97\xcd\x18\x8b\xc2\xa8\n\xec\x04\xceOY\xc0\xfcN\xc9\x13uY\x82;.\x11Lo\xe6\x8bj"b\xd6\x82\xac\x07e8"\x94b\xfc\xc1\xdf.R.\x92\xf3)\x97\x8b=\x13\xe7\xad\x9f\xc9\x82OS\xe1\x99\xc2x\xc64\xb1\xa1v\x04X\x16\x0e4B\x97\xb9q|\x01jN3\xbb\x82\x1f\x9d\xd8F\xb4a\x8f\xdd\x14,B_q\xc7\x93\x15B\xd2:\x14\xb8E.\\\xd6hY\x04.\x7f\x07\xff\x01\x986\xed\x80\x16\xf12<\x00\x00\x00\x00IEND\xaeB`\x82'
--------html_content---------

				"""

                f.close()
                # 2.1 准备发送给浏览器的数据---header
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                # 2.2 准备发送给浏览器的数据---boy
                # response += "hahahhah"

                # 将response header发送给浏览器
                new_socket.send(response.encode("utf-8"))
                # 将response body发送给浏览器
                new_socket.send(html_content)

        else:
            # 2.2 如果是以.py结尾,那么就认为是动态资源的请求
            env = dict()
            body = mini_frame.application(env, self.set_response_header)    # return值

            header = "HTTP/1.1 %s\r\n" % self.status

            for temp in self.headers:
                header += "%s:%s\r\n" % (temp[0], temp[1])

            header += "\r\n"

            response = header+body
            # print('################')
            # print('response>>>:', '\n', response)
            # # response打印结果:
            # """
            # response>>>:
            #  HTTP/1.1 200 OK
            # server:mini_web v8.8
            # Content-Type:text/html;charset=utf-8
            #
            # Hello World! 我爱你中国....
            # """
            # print('################')
            # print('header>>>:', '\n', header)
            # # header打印结果:
            # """
            # header>>>:
            #  HTTP/1.1 200 OK
            # server:mini_web v8.8
            # Content-Type:text/html;charset=utf-8
            #
            # """
            # print('################')
            # print('body>>>:', '\n', body)
            # # body打印结果:
            # """
            # body>>>:
            #  Hello World! 我爱你中国....
            # """

            # 发送response给浏览器
            new_socket.send(response.encode("utf-8"))


        # 关闭套接
        new_socket.close()
    def service_client(self, new_socket):
        "为一个客户端进行服务,为这个客户端返回数据"

        # 1. 接收浏览器发送过来的请求,即HTTP请求
        request_data = new_socket.recv(1024).decode("utf-8")
        # 将请求报文以行分隔为列表
        request_header_lines = request_data.splitlines()
        # 格式化打印出请求报文信息,换行打出
        for line in request_header_lines:
            print(line)

        # 提取出请求网页的名称,即/后面的内容
        # 先取出请求头的第一行
        request_line = request_header_lines[0]
        # 匹配出/之外的任何字符,就是从GET开始匹配,然后从后面的/之后视为一个分组
        # 匹配出出空格外的任何内容,即从第一个/开始匹配到空格结束
        # 请求头的第一行request_line:GET /index.html HTTP/1.1
        # 匹配结果:GET /index.html 我们提取出/及以后的内容
        # [^/]+表示匹配除了/以为的任何字符多次,/[^ ]*表示从/开始匹配任何字符,+匹配1次或多次,*匹配0次或多次
        file_name = re.match("[^/]+(/[^ ]*)", request_line).group(1)
        # 加入网页所在的系统路径,网页都是放在html文件夹中,此时html是放在上级目录,上上级使用../../html
        html_file_name = "../html" + file_name
        print("file name is ===>%s" % file_name)
        print('*' * 50)

        # 2. 返回http格式的数据给浏览器
        # 返回header(http协议内容)和body(浏览器要显示的内容)

        # 2.1 如果请求的资源不是以.py结尾,就认为是静态资源(比如html,css,js,png,jpg等)
        if not file_name.endswith('.py'):
            # 请求的网页也可能不存在,加入try语句
            try:
                f = open(html_file_name, 'rb')

            except:
                # 如果请求的页面不能打开,即不存在,返回以下信息
                response_header = "HTTP/1.1 404 not found\r\n"
                response_header += "\r\n"
                response_body = "====sorry ,file not found===="
                response = response_header + response_body
                new_socket.send(response.encode("utf-8"))

            else:
                # 页面存在,则返回相应的页面信息
                # 2.1.1 组织响应头信息(header),浏览器中换行使用\r\n
                response_header = "HTTP/1.1 200 OK\r\n"  # 200表示找到这个资源
                response_header += "\r\n"  # 用一个空的行与body进行隔开,作为换行符
                # 组织内容(body)
                # 返回一个本地已经编辑好的前端html页面
                response_body = f.read()
                f.close()
                # 2.1.2 组织响应报文,发送数据,由于已经不是单纯的字符串,不能使用拼接
                # 头和体信息单独发送,或者编码后一起发送
                response = response_header.encode("utf-8") + response_body
                new_socket.send(response)

        else:
            # 2.2 如果是以.py结尾,那么就认为是动态资源请求
            # 动态请求,就调用mini_frame框架
            # 以下代码就实现了Web服务器支持MSGI协议
            # 代码执行流程:查看mini_frame.py中的说明
            env = dict()
            response_body = mini_frame.application(env,
                                                   self.set_response_header)

            response_header = 'HTTP/1.1 %s\r\n' % self.status
            # 提取出self.headers里面的信息,添加到response_header中去
            # headers是一个列表,每个元素又是一个元组,取出列表的第一个元素和第二个元素,加上冒号,就变成了了html的格式了
            for temp in self.headers:
                response_header += '%s:%s\r\n' % (temp[0], temp[1])
            # header和body之间加上空行
            response_header += "\r\n"
            # 浏览器默认解析使用的是gbk,gbk可以显示更多的汉字,此处直接使用utf-8会乱码
            # Content-Type里面指定charset编码是utf-8就可以完美显示中文了
            response = response_header + response_body

            new_socket.send(response.encode("utf-8"))

        # 3. 关闭客户端套接字
        new_socket.close()