NO.2 Python Web 服务器

NO.2 Python Web 服务器

零蚀


浏览器访问服务器

  • 返回Web静态页面

    • Client-HTTP请求的方式
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 连接TCP服务器
    tcp_socket.connect(("tangyangapp.com", 80))
    # 发送TCP请求(HTTP协议)
    # 请求行
    req_line = "GET / HTTP/1.1\r\n"
    # 请求头
    req_head = "User-Agent: HmBrowser/5.0\r\n"
    # 空行
    req_empty = "\r\n"
    
    req_data = (req_line + req_head + req_empty).encode()
    tcp_socket.send(req_data)
    recv = tcp_socket.recv(1024 * 4)
    print(recv.decode())
    
    tcp_socket.close()
    
    • 响应数据
    HTTP/1.1 400 Bad Request
    Server: nginx
    Date: Sat, 01 Feb 2020 04:28:19 GMT
    Content-Type: text/html
    Content-Length: 150
    Connection: close
    \r\n
    <html>
    <head><title>400 Bad Request</title></head>
    <body>
    <center><h1>400 Bad Request</h1></center>
    <hr><center>nginx</center>
    </body>
    </html>
    

    截出请求体:

    index = recv.decode().find("\r\n\r\n")
    body = recv.decode()[index + 4:]
    
  • Server-HTTP返回固定数据

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(("", 8886))
    server_socket.listen(1024)
    while True:
        client, ip_port = server_socket.accept()
    
        recv = client.recv(1024 * 4).decode()
        # 状态行
        line = "HTTP/1.1 200 OK\r\n"
        # 响应头
        head = "server:ZERO/1.5\r\n"
        # 响应体
        body = "hello world"
        client.send((line + head + "\r\n" + body).encode())
    client.close()
    
    • Server返回固定页面

      • 导入静态网站
        在这里插入图片描述

      • 代码部分

      server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      server_socket.bind(("127.0.0.1", 8886))
      server_socket.listen(1024)
      while True:
          client, ip_port = server_socket.accept()
      
          recv = client.recv(1024 * 4).decode()
          # 状态行
          line = "HTTP/1.1 200 OK\r\n"
          # 响应头
          head = "server:ZERO/1.5\r\n"
          head += "Content-Type: text/html;charset=utf-8\r\n"
          # 响应体
          with open("static/index.html", "rb") as file:
              body = file.read()
      
          client.send((line + head + "\r\n").encode() + body)
      client.close()
      

      with open() as file 的优点 一是为了代码简洁,二是节省了file.close() 的方法。

      • 访问获取:
        在这里插入图片描述

      这里我们也可以用请求的recv信息截取到path,来指定打开哪一个html,例如:127.0.0.1:8886/index2.html访问。

      如果用这种方式来截取路径,来指定页面,需要try…catch…

  • Jinja

    • 安装
    pip install jinja2
    
    • 替换网页内容
    from jinja2 import Environment, FileSystemLoader
    
    t_env = Environment(loader=FileSystemLoader("./templates/"))
    
    template = t_env.get_template("index.html")
    
    data = template.render(
    content1="aaaa"
    )
    
    

    data是将index网页的的{{ content1 }}字符换置成content1赋值内容aaaa。

    • 防止端口占用,释放端口
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SOCK_STREAM, True)
    

  • 动态资源请求WSGI

    • 图解
    1.发送请求index.html
    2.解析index.html
    3.读取index.html
    4.返回index.html
    静态资源服务器
    浏览器
    HTTP服务器
    硬盘
    1.发送请求index.html
    2.解析html/css/png等资源
    3.读取html/css/png等资源
    4.返回index.html
    动态请求
    动态数据
    动态资源服务器
    浏览器
    HTTP服务器
    硬盘
    web框架

    动态页面一般有 .jsp .php 等

    • 多进程
    import multiprocessing
    def func(args):
        pass
    
    # 申请一个进程
    multi = multiprocessing.Process(target=func(),args=("arg",))
    # 开启进程
    multi.start()
    

🔗 前言
🔗 Python 高级列表
🔗 NO.1 Python网络通信
🔗 NO.3 Python多线程&锁
🔗 NO.4 Python多进程
🔗 NO.5 Python协程&生成器
🔗 NO.6 Python正则&爬虫初
🔗 NO.7 Python操作MySQL
🔗 NO.8 Python Spider
🔗 NO.9 Python 装饰器&other

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零蚀zero eclipse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值