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客户端。

  • 相关阅读:
    Python 模拟SQL对文件进行增删改查
    Python用户登陆
    计算程序的内存和占比
    列出top中的pid
    编写类du命令Python脚本
    生成器版本的文件MD5校验
    利用os、hash模块生成目录下所有文件的md5
    文件Copy和文件夹Copy
    Access数据库连接方式
    js常用方法收集
  • 原文地址:https://www.cnblogs.com/share100/p/6926475.html
Copyright © 2011-2022 走看看