通过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\r\nHost:{}\r\nConnection:close\r\n\r\n".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("\r\n\r\n")[1:]
    html = "\r\n\r\n".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())

 

posted on 2021-01-11 20:20  文艺青年.茶  阅读(145)  评论(0编辑  收藏  举报

导航