zoukankan      html  css  js  c++  java
  • 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客户端。

  • 相关阅读:
    aa
    ECS上搭建Docker(CentOS7)
    mysql时间戳转日期
    rsync用法
    docker安装mysql8
    使用Docker安装mysql,挂载外部配置和数据
    my.cnf
    Centos7通过yum安装jdk8
    maven添加本地包命令mvn install:install-file
    Mysql——查看数据库,表占用磁盘大小
  • 原文地址:https://www.cnblogs.com/share100/p/6926475.html
Copyright © 2011-2022 走看看