Python基础--函数版搭建简单WebServer(动静分离)

2 篇文章 0 订阅

Python基础–函数版搭建简单WebServer(动静分离)

大致代码思路如下
1.初始化socket
2.端口复用
3.bind端口
4.listen打开监听
5.循环接受请求
6.处理客户请求
7.发送客户请求
8.关闭服务客户端的端口
9.关闭服务端

  • 端口号是:8080
  • IP地址是:127.0.0.1
  • 路径分别为127.0.0.1:8080, 127.0.0.1/post.htm, :
  • post.html这个页面的代码是自己写的超级简单的html页面,如果不知道的话,在最后
    Tips:这个代码是面向函数版的,之后会把面向对象的写出来,当前代码,经过了去重(重构),函数化和动静分离.所以如果有需要的可以在详细看下
import socket

def client_exec(client):
    # 获取接受的数据
    data = client.recv(1024)

    # 看看data是否有数据.有数据的时候才会处理,没数据直接退出客户端
    if data:
        # 对数据进行解码
        decode_data = data.decode('utf-8')
        print(decode_data)

        # 对获取到的数据进行切割,得到对应的访问路径
        split_data = decode_data.split(' ', maxsplit=2)

        # 异常判断,如果切割数据有异常,则直接关闭客户端.
        # 因为服务端不清楚,是否正确获取到路径,以防客户输入有误,或者放置入侵添加异常处理
        try:
            file_path = split_data[1]
            if file_path == "/":
                file_path = "/index.html"
        except Exception as e:
            print("异常是 ", e)
            client.close()
            return

    else:
        client.close()
        return

        # 进行动静分离.目前首先以后缀名进行区分
        # 简单区分:
        # 动态文件: html
        # 静态文件: 图片,音频,视频,大文本
        # 使用函数,endswith取出后缀名进行判断
        # 这里暂时只设置html是动态资源,其他都是静态.如果有其他需求的话,可以再添加elif进行判断.或者使用正则表达式进行比配也行
    if file_path.endswith(".html"):

		# 代码重构,把指定的重复部分,添加至头部
        reponse_line = "http/1.1 200 OK\r\n"
        reponse_header = ""
        reponse_empty = '\r\n'
        # 根据获取的数据进行返回内容
        if file_path == '/post.html':
			# 异常判断,打开文件,如果文件异常,返回异常信息,关闭连接
			# 这个是对应html文件的打开,只写了一个例子,下面elif都是文本输出
            try:
                with open("./123.html", encoding='utf-8') as f:
                    reponse_body = f.read()
            except Exception as e:
                print("123.html异常:", e)
                client.close()
                return
                
        elif file_path == '/index.html':
            reponse_header = "content-type:text/html;charset= utf-8\r\n"
            reponse_body = '你好,这个是index界面'
        else:
	        # 设置404错误界面,如果不是匹配上面的,就直接放回404页面
            reponse_line = "http/1.1 404 not found\r\n"
            # 回应头,添加指定的编码信息
            reponse_header = "content-type:text/html;charset= utf-8\r\n"
            # 空行
            reponse_empty = "\r\n"
            reponse_body = '你好,这个是错误界面'
            
        # 代码重构(去重)将冗余代码进行缩减,填写至公共部分
        reponse_conten = reponse_line + reponse_header + reponse_empty + reponse_body
        client.send(reponse_conten.encode("utf-8"))
        client.close()
        return
    else:
        # file_path.endswith(".jpg|png|jpeg"):
        del_image(client, file_path)

    client.close()


def del_image(client, file_path):
    """
    静态资源处理
    :param client: 传参的客户端所有信息
    :param file_path: 客户端的访问路径
    :return:
    """
    try:
        reponse_line = "http/1.1 200 OK \r\n"
        # 回应头
        reponse_header = ""
        # 空行
        reponse_empty = '\r\n'
        # 打开文件
        with open('.%s' % file_path, "rb") as f:
            creponse = f.read()
        reponse_conten = reponse_line.encode("utf-8") + reponse_header.encode("utf-8") + reponse_empty.encode(
            "utf-8") + creponse
        client.send(reponse_conten)
    except Exception as e:
        print("错误是:", e)
        reponse_404_html(client)


def reponse_404_html(client):
    """
    设置返回404的界面
    :param client:
    :return:
    """
    reponse_line = "http/1.1 404 not found\r\n"
    # 回应头
    reponse_header = "content-type:text/html;charset= utf-8\r\n"
    # 空行
    reponse_empty = "\r\n"
    reponse_body = '错误'
    reponse_conten = reponse_line + reponse_header + reponse_empty + reponse_body
    client.send(reponse_conten.encode("utf-8"))


def del_client(server_socket):
    # 循环接受处理客户请求
    while True:
        # 获取指定的客户端信息和地址
        client, address = server_socket.accept()
        print(client)

        # 处理客户请求
        client_exec(client)


def init_tcp():
    """
    初始化socket,设置端口复用
    :return: server_socket
    """
    # 初始化socket
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 设置端口复用
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
    # bind端口
    server_socket.bind(('', 8080))
    # 设置端口监听
    server_socket.listen(128)
    return server_socket


def main():
    """
    主函数运行程序
    :return:none
    """
    # 初始化tcp套接字
    server_socket = init_tcp()

    # 处理客户换请求
    del_client(server_socket)

    # 关闭客户端
    server_socket.close()


if __name__ == '__main__':
    main()

html语言的代码,.超级简单,不喜勿喷

<!DOCTYPE html>
<html>
<head>
    <title>post</title>
</head>
<body>
    <center>
        <form action="http://127.0.0.1:8080" method="post">
            <p>name:<input type="text" name="name"/></p>
            <p>password<input type="password" name="pwd"/></p>
            <input type="submit" value="login">

        </form>
    </center>
    <!--图片的展示-->
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值