zoukankan      html  css  js  c++  java
  • 通过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())
  • 相关阅读:
    2019春总结作业
    第十周作业
    第九周作业
    第八周作业
    第七周作业
    202103226-1 编程作业
    1 20210309-2 阅读任务
    1 20210309-1 准备工作
    课程总结
    第十三周总结
  • 原文地址:https://www.cnblogs.com/yejing-snake/p/14264182.html
Copyright © 2011-2022 走看看