Python 实现静态web服务器

版权声明:(●◡●) https://blog.csdn.net/wumingid/article/details/89322072

HTTP协议简介

HTTP请求

1:浏览器首先向服务器发送HTTP请求,请求包括:

方法:GET还是POST,GET仅请求资源,POST会附带用户数据;
路径:/full/url/path;
域名:由Host头指定:Host: www.sina.com以及其他相关的Header;
如果是POST,那么请求还包括一个Body,包含用户数据

2:服务器向浏览器返回HTTP响应,响应包括:

响应代码:200表示成功,3xx表示重定向,4xx表示客户端发送的请求有错误,5xx表示服务器端处理时发生了错误;
响应类型:由Content-Type指定;
以及其他相关的Header;
通常服务器的HTTP响应会携带内容,也就是有一个Body,包含响应的内容,网页的HTML源码就在Body中。

3:如果浏览器还需要继续向服务器请求其他资源,比如图片,就再次发出HTTP请求,重复步骤1、2。

Web采用的HTTP协议采用了非常简单的请求-响应模式,从而大大简化了开发。当我们编写一个页面时,我们只需要在HTTP请求中把HTML发送出去,不需要考虑如何附带图片、视频等,浏览器如果需要请求图片和视频,它会发送另一个HTTP请求,因此,一个HTTP请求只处理一个资源(此时就可以理解为TCP协议中的短连接,每个链接只获取一个资源,如需要多个就需要建立多个链接)

HTTP格式

每个HTTP请求和响应都遵循相同的格式,一个HTTP包含Header和Body两部分,其中Body是可选的。
HTTP协议是一种文本协议,所以,它的格式也非常简单。

1 HTTP GET请求的格式:

GET /path HTTP/1.1
    Header1: Value1
    Header2: Value2
    Header3: Value3

每个Header一行一个,换行符是\r\n。

2 HTTP POST请求的格式:


 POST /path HTTP/1.1
    Header1: Value1
    Header2: Value2
    Header3: Value3

    body data goes here...

3 HTTP响应的格式:


200 OK
    Header1: Value1
    Header2: Value2
    Header3: Value3

    body data goes here...

HTTP响应如果包含body,也是通过\r\n\r\n来分隔的。
请再次注意,Body的数据类型由Content-Type头来确定,如果是网页,Body就是文本,如果是图片,Body就是图片的二进制数据。
当存在Content-Encoding时,Body数据是被压缩的,最常见的压缩方式是gzip,所以,看到Content-Encoding: gzip时,需要将Body数据先解压缩,才能得到真正的数据。压缩的目的在于减少Body的大小,加快网络传输。

demo:静态web服务器

import socket
import re
import time


def service_client(new_socket):
    """为这个客户端服务"""

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

    # 判断客户端意外断开链接返回空字符串
    if not request:
        # 关闭套接字并退出
        new_socket.close()
        print("==="*30)
        return

    # 分隔套接字
    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"

    # 2.返回http格式数据 给浏览器
    try:
        f = open("./html" + file_name, "rb")
    except:
        response = "HTTP/1.1 404 NOT FOUND\r\n"
        response += "Content-Type:text/html;charset=utf-8\r\n"
        response += "\r\n"
        response += "<h1>404 not found <br> 没有发现所请求资源</h1>"
        response += str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
        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 header发送给浏览器
        new_socket.send(response.encode("utf-8"))
        # 将response body发送给浏览器
        new_socket.send(html_content)

    # 关闭套接字
    new_socket.close()


def main():
    """用来完成整体的控制"""
    # 1.创建套接字
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 端口复用
    tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    # 2.绑定
    tcp_server_socket.bind(("", 7890))
    # 3.变为套接字
    tcp_server_socket.listen(128)

    while True:
        # 4.等待客户端的链接
        new_socket, client_addr = tcp_server_socket.accept()

        print(client_addr)

        # 5.为这个客户端服务
        service_client(new_socket)

    # 关闭监听套接字
    tcp_server_socket.close()


if __name__ == '__main__':
    main()

exit((。>︿<)_θ)

猜你喜欢

转载自blog.csdn.net/wumingid/article/details/89322072