通过asyncio模拟HTTP请求

import asyncio
from urllib.parse import urlparse


# 通过socket请求html
# asyncio完成http请求
async def get_url(url):
    url = urlparse(url)
    host = url.netloc
    path = url.path
    if path == "":
        path = "/"

    # 建立socket连接
    reader, writer = await asyncio.open_connection(host, 80)

    writer.write("GET {} HTTP/1.1
Host:{}
Connection:close

".format(path, host).encode("utf8"))

    all_lines = []
    async for raw_line in reader:
        data = raw_line.decode("utf8")
        all_lines.append(data)

    data = "".join(all_lines)
    html = data.split("

")[1:]
    html = "

".join(html)
    return html


async def main():
    tasks = [asyncio.ensure_future(get_url("http://www.baidu.com"))]
    for task in asyncio.as_completed(tasks):
        result = await task
        print(result)

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
原文地址:https://www.cnblogs.com/yejing-snake/p/14264182.html