Example #1
0
def application(current_dir, request_data, ip_port):

    file_path = parse_request(request_data, ip_port)
    resource_path = current_dir + file_path
    response_data = ""
    #dynamic
    if file_path.endswith(".py"):
        if file_path in urls.route_dict:
            response_body = urls.route_dict[file_path]().encode()
            response_data = utils.create_http_response("200 OK", response_body)
        else:
            response_body = "This page Not Found!".encode()
            response_data = utils.create_http_response("404 Not Found",
                                                       response_body)
    else:
        #static
        try:
            with open(resource_path, "rb") as file:
                response_body = file.read()

                response_data = utils.create_http_response(
                    "200 OK", response_body)

        except Exception as e:

            response_body = f"Error! {str(e)}"
            response_body = response_body.encode()
            response_data = utils.create_http_response("404 Not Found",
                                                       response_body)

    return response_data
Example #2
0
def application(current_dir,new_client_socket,ip_port):
    #定义变量保存资源路径
    file_path,gaes =parse_request(new_client_socket,ip_port)
    resource_path = current_dir + str(file_path)
    #改进动态判断后缀
    if file_path.endswith(".py"): #让.py 和 .html 的内容区别开
        #根据不同内容显示不同的资源内容
        if file_path in urls.route_dict:
            response_body =urls.route_dict[file_path](gaes)
            request_data = utils.create_http_response("200 OK", response_body.encode())  # 调用utils模块处理拼接响应协议

        else:
            response_body = "Sorry page Not Found! 404".encode()
            request_data = utils.create_http_response("404 Not Found", response_body)  # 调用utils模块处理拼接响应协议


    else:

        try:
            with open(resource_path, "rb") as file:
                response_body = file.read()
                request_data=utils.create_http_response("200 OK",response_body)#调用utils模块处理拼接响应协议
        except Exception as e:
            response_body = "Error!{}".format(str(e)).encode()
            request_data = utils.create_http_response("404 Not Found", response_body)  # 调用utils模块处理拼接响应协议
    return request_data
Example #3
0
def application(current_dir, request_data, ip_port):

    # 调用 parse_request函数,解析请求协议,返回请求的资源路径
    file_path = parse_request(request_data, ip_port)
    # 定义变量保存资源路径
    resource_path = current_dir + file_path

    try:
        # 通过 with open 读取文件
        with open(resource_path, "rb") as file:
            # 把读取的文件内容返回给客户端
            response_body = file.read()

        # 调用 utils 模块的 create_http_response 函数,拼接响应协议
        response_data = utils.create_http_response("200 OK", response_body)

    except Exception as e:

        # 2)响应的内容为错误
        response_body = "Error! (%s)" % str(e)
        # 3)把内容转换为字节码
        response_body = response_body.encode()
        # 调用 utils 模块的 create_http_response 函数,拼接响应协议
        response_data = utils.create_http_response("404 Not Found",
                                                   response_body)

    return response_data
Example #4
0
def application(request_data,ip_port):

    # 调用parse_request函数,解析请求协议,返回请求的资源路径.
    file_path = parse_request(request_data,ip_port)
    indexpath = "F:\\PythonProject\\python-scripts\\DevOps\\network\\tcpWeb\\static\\index.html"

    # resource_path = os.path.join(os.getcwd() + "python-scripts\\DevOps\\network\\tcpWeb\\" + current_dir + "\\\\" + file_path

    try:
        # 返回固定页面, 通过with读取文件.
        with open(indexpath,"rb") as file:
            # 把读取的文件内容返回给客户端.
            response_body = file.read()

        # 调用utils模块的create_http_response 函数,拼接响应协议
        response_data = utils.create_http_response("200 ok",response_body)

    except Exception as e:
        # 重新修改响应行为404
        response_line = "HTTP/1.1 404 Not Found\r\n"
        # 响应的内容为错误.
        response_body = "Error! {}".format(str(e))
        # 把内容转换为字节码
        response_body = response_body.encode()

        # 
        response_data = utils.create_http_response("404 Not Found",response_body)
        

    return response_data
Example #5
0
def application(current_dir, request_data, ip_port):

    file_path = parse_requset(request_data)

    resource_path = current_dir + file_path

    response_data = ""

    if file_path.endswith(".html"):

        # print(file_path)
        #     # if file_path in urls.route_dict:

        for key, values in urls.route_dict.items():
            print(key + "-------->")
            print(file_path)
            ls = re.match(key, file_path)
            # print(ls)
            if ls:
                # func = urls.route_dict[file_path]
                func = values

                response_body = func(file_path)

                response_data = utils.create_http_response(
                    "200 Ok", response_body.encode())

                break

            else:
                response_body = "Sorry page Not Found ! 404"

                response_data = utils.create_http_response(
                    "404 Not Found", response_body.encode())

    else:
        try:

            with open(resource_path, "rb") as file:

                response_body = file.read()

            response_data = utils.create_http_response("200 OK", response_body)

        except Exception as rs:

            response_body = "Error! (%s)" % str(rs)

            response_body = response_body.encode()

            response_data = utils.create_http_response("404 Not Found",
                                                       response_body)

    return response_data
Example #6
0
def application(curr_dir, request_data):
    request_line_dict = parse_request(request_data)
    path_info = curr_dir + request_line_dict['file_path']

    try:
        with open(path_info, "rb") as file:
            response_body = file.read()
            response_data = utils.create_http_response("200 OK", response_body)
    except Exception as e:
        response_body = "Error !! %s" % str(e)
        response_body = response_body.encode()
        response_data = utils.create_http_response("404 Not Found",
                                                   response_body)

    return response_data
Example #7
0
def application(current_dir, request_data, ip_port):
    file_path = parse_request(request_data, ip_port)
    resource_path = current_dir + file_path
    try:
        #通过withopen读取文件
        with open(resource_path, "rb") as file:
            response_body = file.read()
            #调用utils模块的这个函数 拼接响应协议
        response_data = utils.create_http_response("200 OK", response_body)

    except Exception as e:
        response_body = "Error! (%s)" % str(e)
        #将字符串转换为二进制
        response_body = response_body.encode()

        #--响应全体拼接
        #调用utils模块的这个函数 拼接响应协议
        response_data = utils.create_http_response("404 Not found",
                                                   response_body)

    return response_data
Example #8
0
def application(static, request_data, ip_port):
    # 解析请求报文 得到请求信息字典
    request_line_dict = parse_requsest(request_data)
    path_info = 'static' + request_line_dict['file_path']
    response_data = ""
    # 判断文件是否是python文件
    if request_line_dict['file_path'].endswith(".py"):
        # 如果是python动态文件 做单独处理
        # 判断当前访问路径是否在路由列表中
        if request_line_dict['file_path'] in urls.route_dict:
            # 调用对应函数
            func = urls.route_dict[request_line_dict['file_path']]
            # 调用函数执行 并且得到返回值
            response_body = func()
            # 创建HTTP协议
            response_data = utils.create_http_response("200 OK",
                                                       response_body.encode())
        else:
            response_data = utils.create_http_response("404 Not Found",
                                                       "Not Found")

    else:
        try:
            # 如果报错 则执行此代码
            with open(path_info, "rb") as file:
                response_body = file.read()
                # 调用方法 得到响应报文
                response_data = utils.create_http_response(
                    "200 OK", response_body)
        except Exception as e:
            # 重新设置请求行
            response_line = "HTTP/1.1 404 Not Found\r\n"
            # 重新设置响应信息为错误信息
            response_body = "Error!! %s" % str(e)
            response_body = response_body.encode()
            response_data = utils.create_http_response("404 Not Found",
                                                       response_body)
    return response_data