1. 程式人生 > 實用技巧 >非阻塞IO HTTP請求

非阻塞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\r\nHost:{}\r\nConnection:close\r\n\r\n".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("\r\n\r\n")[1:] print("\r\n\r\n".join(html_data)) client.close()
if __name__ == "__main__": get_url("http://www.baidu.com")