• tcp与http协议 以及python的实现


    htpp协议 Rquest Headers格式:

      请求方法空格URL空格协议版本回车符换行符

      头部字段名:值回车符换行符

      ···

      头部字段名:值回车符换行符

      回车符换行符

      请求数据

    socket网络聊天

    服务端

    #sokcet服务断
    import socket
    import threading
    server = socket.socket()
    # 绑定到0.0.0.0:8000端口上
    server.bind(('0.0.0.0',8000)) # 如果是绑定127.0.0.1,那么只能本地连接
    server.listen()
    
    def handle_sock(sock,addr):
        while True:
            # recv是阻塞的
            tmp_data = sock.recv(1024)
            print(tmp_data.decode("utf8"))
            input_data = input()
            sock.send(input_data.encode("utf8"))
    # 获取客户端连接并启动线程去处理
    while True:
        # 阻塞,等待连接
        sock,addr = server.accept()
    
        # 启动一个线程去处理新的用户连接
        client_thread = threading.Thread(target=handle_sock,args=(sock,addr))
        client_thread.start()
    # sock.close()

    客户端

    # socket客户端
    import socket
    client = socket.socket()
    client.connect(('192.168.2.129',8000))
    while True:
        
        input_data = input()
        client.send(input_data.encode('utf8'))
        server_data = client.recv(1024)
        print(server_data.decode('utf8'))
    
    client.close()

    socket模拟http实现拉取百度首页源码:

    # socket客户端
    import socket
    http_client = socket.socket()
    http_client.connect(('www.baidu.com',80))
    html = "GET / HTTP/1.1
    Connection:close
    
    "
    http_client.send(html.encode("utf8"));
    
    data = b""
    while True:
        tmp = http_client.recv(1024)
        if tmp:
            data += tmp;
        else:
            break
    print(data.decode('utf8'))
    input()
    
        

    socket模拟http实现nginx接受访问:

    #tcp实现http协议
    import socket
    server = socket.socket()
    
    server.bind(('0.0.0.0',80))
    server.listen()
    # 获取客户端连接并启动线程去处理
    while True:
        print("等待连接")
        # 阻塞,等待连接
        sock,addr = server.accept()
        tmp_data = sock.recv(1024)
        print("收到浏览器信息:")
        print(tmp_data.decode("utf8"))
        # 谷歌浏览器访问 打印为:
        #     GET / HTTP/1.1
        #     Host: 192.168.2.129
        #     Connection: keep-alive
        #     Cache-Control: max-age=0
        #     Upgrade-Insecure-Requests: 1
        #     User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36
        #     Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
        #     Accept-Encoding: gzip, deflate
        #     Accept-Language: zh-CN,zh;q=0.9
        response_template = '''HTTP/1.1 200 OK
    
        <h1>hello</h1>'''
        sock.send(response_template.encode("utf8"))
        # 浏览器展示形式为:
        #     Request URL: http://192.168.2.129/
        #     Request Method: GET
        #     Status Code: 200 OK
        #     Remote Address: 192.168.2.129:80
        #     Referrer Policy: no-referrer-when-downgrade
        print("已回信息")
        sock.close()
        print("已断开连接")

    Rquest Headers

  • 相关阅读:
    正向代理与反向代理的区别
    显式拥塞通知ECN剖析
    Save info in Hidden Field
    sessionID difference
    Windows server 2003 无法找到Cookies位置
    create Cookie
    SQL Server Session
    New class in ASP.NET
    Save info in Query string
    Crypto number
  • 原文地址:https://www.cnblogs.com/cl94/p/12270218.html
Copyright © 2020-2023  润新知