• web本质


    python-socket服务器

    import socket
    
    def handle_request(client):
        buf = client.recv(1024)
        client.send(bytes("HTTP/1.1 200 OK
    
    ",encoding="UTF-8"))
        client.send(bytes("hello,coco~!",encoding="UTF-8"))
    
    def main():
        sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        sock.bind(('localhost',8090))
        sock.listen(2)
    
        while True:
            connection,address = sock.accept()
            handle_request(connection)
            connection.close()
    
    if __name__ == '__main__':
        main()
    

    将输入的字符串,可以写到单独的文件里 #index

    <h1 style='background-color:pink'>hello,coco~!</h1>
    <h1 style='background-color:gray'>hello,yoyo~!</h1>
    

    后端程序读取,并赋值给变量

    import socket
    
    def handle_request(client):
        buf = client.recv(1024)
        client.send(bytes("HTTP/1.1 200 OK
    
    ",encoding="UTF-8"))
        f = open('index','rb')
        data = f.read()
        f.close()
        client.send(data)
    
    def main():
        sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        sock.bind(('localhost',8090))
        sock.listen(2)
    
        while True:
            connection,address = sock.accept()
            handle_request(connection)
            connection.close()
    
    if __name__ == '__main__':
        main()
    

    效果:

     总结:所有的web服务器(nginx、apache...)本质上都是socket服务器,浏览器充当的就是socket客户端。

  • 相关阅读:
    【解读】Https协议
    【解读】Http协议
    tomcat中AJP协议和HTTP协议的区别
    TOMCAT原理详解及请求过程
    Redis持久性——RDB和AOF
    redis配置文件解读
    HttpClient优化
    crontab与系统时间不一致
    天兔(Lepus)监控操作系统(OS)安装配置
    MySQL 优化之 index_merge (索引合并)
  • 原文地址:https://www.cnblogs.com/share100/p/6926475.html
Copyright © 2020-2023  润新知