zoukankan      html  css  js  c++  java
  • 非阻塞IO HTTP请求

    import socket
    from urllib.parse import urlparse
    
    
    # 通过socket请求html
    # 非阻塞IO完成http请求
    def get_url(url):
        url = urlparse(url)
        host = url.netloc
        path = url.path
        if path == "":
            path = "/"
    
        # 建立socket连接
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.setblocking(False)
        try:
            client.connect((host, 80))
        except BlockingIOError as e:
            pass
    
        while True:
            try:
                client.send("GET {} HTTP/1.1
    Host:{}
    Connection:close
    
    ".format(path, host).encode("utf8"))
                break
            except OSError as e:
                pass
    
        data = b""
        while True:
            try:
                d = client.recv(1024)
            except BlockingIOError as e:
                continue
            if d:
                data += d
            else:
                break
    
        data = data.decode("utf8")
        html_data = data.split("
    
    ")[1:]
        print("
    
    ".join(html_data))
        client.close()
    
    
    if __name__ == "__main__":
        get_url("http://www.baidu.com")
  • 相关阅读:
    type和object详解
    元类+单例
    单表查询和多表查询
    外键
    存储引擎,MySQL中的数据类型及约束
    壹拾壹




  • 原文地址:https://www.cnblogs.com/yejing-snake/p/14263786.html
Copyright © 2011-2022 走看看