import socket from urllib.parse import urlparse from selectors import DefaultSelector, EVENT_READ, EVENT_WRITE def get_url(url): #通过socket请求html 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: #如果不try的话 则会有异常,因为链接还没建立好 #BlockingIOError: [WinError 10035] 无法立即完成一个非阻止性套接字操作。 client.connect((host, 80)) #阻塞不会消耗cpu except BlockingIOError as e: pass #不停的询问连接是否建立好, 需要while循环不停的去检查状态 #做计算任务或者再次发起其他的连接请求 while True: try: #发送http请求 #请求行\r\n请求头\r\n\r\n请求体 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(html_data) client.close() get_url('http://www.baidu.com')
响应结果:
响应行\r\n
响应头\r\n
...........\r\n
...........\r\n
...........
\r\n\r\n
响应体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | "C:\Program Files\python3.5\python.exe" E:/pythoncode/day07/01.py HTTP/1.1 200 OK Accept-Ranges: bytes Cache-Control: no-cache Content-Length: 14615 Content-Type: text/html Date : Thu, 16 Aug 2018 08:52:32 GMT Etag: "5b56b4a8-3917" Last-Modified: Tue, 24 Jul 2018 05:10:00 GMT P3p: CP= " OTI DSP COR IVA OUR IND COM " Pragma: no-cache Server: BWS/1.1 Set-Cookie: BAIDUID=DCD58464E426E409DBF02803181718DB:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com Set-Cookie: BIDUPSID=DCD58464E426E409DBF02803181718DB; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com Set-Cookie: PSTM=1534409552; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com Vary: Accept-Encoding X-Ua-Compatible: IE=Edge,chrome=1 Connection: close <!DOCTYPE html><!--STATUS OK--> <html> <head> <meta http-equiv= "content-type" content= "text/html;charset=utf-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=Edge" > <link rel= "dns-prefetch" href= "//s1.bdstatic.com" /> <link rel= "dns-prefetch" href= "//t1.baidu.com" /> <link rel= "dns-prefetch" href= "//t2.baidu.com" /> <link rel= "dns-prefetch" href= "//t3.baidu.com" /> <link rel= "dns-prefetch" href= "//t10.baidu.com" /> <link rel= "dns-prefetch" href= "//t11.baidu.com" /> <link rel= "dns-prefetch" href= "//t12.baidu.com" /> <link rel= "dns-prefetch" href= "//b1.bdstatic.com" /> |
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/9488322.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能