当前位置: 首页 > wzjs >正文

怀化汽车网站做网站需要服务器吗

怀化汽车网站,做网站需要服务器吗,sae wordpress 媒体库,工业设计网站导航一、发展历程 起源 Requests 库由 Kenneth Reitz 于 2011 年创建,目标是替代 Python 标准库中的 urllib2,提供更简洁、人性化的 HTTP 客户端接口。其设计哲学是 “HTTP for Humans”。 关键版本 2011: v0.2.0 发布,支持 GET/POST 方法。2012: v1.0.0 正式发布,新增 Session…
一、发展历程
  1. 起源
    Requests 库由 Kenneth Reitz 于 2011 年创建,目标是替代 Python 标准库中的 urllib2,提供更简洁、人性化的 HTTP 客户端接口。其设计哲学是 “HTTP for Humans”。

  2. 关键版本

    • 2011: v0.2.0 发布,支持 GET/POST 方法。
    • 2012: v1.0.0 正式发布,新增 Session 对象、流式下载等功能。
    • 2014: v2.0.0 支持 Python 3,底层依赖 urllib3 库。
    • 2022: 移交至 Python Software Foundation 维护。

二、基础方法与核心功能
import requests# 1. GET 请求
response = requests.get(url="http://httpbin.org/get",params={"key": "value"},  # 查询参数headers={"User-Agent": "demo"},  # 请求头timeout=5  # 超时设置
)
print(response.text)  # 响应文本# 2. POST 请求(表单数据)
response = requests.post(url="http://httpbin.org/post",data={"name": "Alice"}  # 表单数据
)# 3. POST 请求(JSON 数据)
response = requests.post(url="http://httpbin.org/post",json={"name": "Bob"}  # 自动设置 Content-Type 为 application/json
)# 4. 文件上传
files = {"file": open("test.txt", "rb")}
response = requests.post("http://httpbin.org/post", files=files)# 5. 处理响应
print(response.status_code)  # HTTP 状态码
print(response.headers)      # 响应头
print(response.json())       # 解析 JSON 数据

三、工作原理与实现机制
  1. 底层依赖
    Requests 基于 urllib3 实现,后者提供连接池、SSL/TLS 验证、重试机制等底层功能。

  2. Session 对象
    使用 Session 复用 TCP 连接,提升性能:

    with requests.Session() as session:session.get("http://httpbin.org/cookies/set/sessioncookie/123")response = session.get("http://httpbin.org/cookies")  # 自动携带 Cookie
    
  3. 请求处理流程

    • 构造 PreparedRequest 对象
    • 通过 adapters 发送请求
    • 返回 Response 对象,封装状态码、头、内容等。

四、应用领域与代码示例
1. 网页抓取(含异常处理)
import requests
from bs4 import BeautifulSouptry:response = requests.get("https://example.com",headers={"User-Agent": "Mozilla/5.0"},  # 模拟浏览器timeout=10)response.raise_for_status()  # 检查 HTTP 错误soup = BeautifulSoup(response.text, "html.parser")print(soup.title.text)
except requests.exceptions.RequestException as e:print(f"请求失败: {e}")
2. REST API 交互
import requests# 获取 GitHub 用户信息
url = "https://api.github.com/users/octocat"
response = requests.get(url)
if response.status_code == 200:data = response.json()print(f"用户名: {data['login']}, 仓库数: {data['public_repos']}")
else:print(f"错误: {response.status_code}")
3. 大文件流式下载
url = "https://example.com/large_file.zip"
response = requests.get(url, stream=True)  # 流式模式with open("large_file.zip", "wb") as f:for chunk in response.iter_content(chunk_size=8192):if chunk:  # 过滤 keep-alive 数据块f.write(chunk)
4. OAuth2 认证
# 使用 OAuth2 获取访问令牌
auth_url = "https://oauth.example.com/token"
data = {"grant_type": "client_credentials","client_id": "YOUR_CLIENT_ID","client_secret": "YOUR_SECRET"
}
response = requests.post(auth_url, data=data)
access_token = response.json()["access_token"]# 携带令牌访问 API
headers = {"Authorization": f"Bearer {access_token}"}
response = requests.get("https://api.example.com/data", headers=headers)
5. 代理设置
proxies = {"http": "http://10.10.1.10:3128","https": "http://10.10.1.10:1080"
}
requests.get("http://example.org", proxies=proxies)

五、高级特性
  1. 自定义适配器
    控制连接池大小:

    from requests.adapters import HTTPAdaptersession = requests.Session()
    adapter = HTTPAdapter(pool_connections=10, pool_maxsize=100)
    session.mount("https://", adapter)
    
  2. 请求钩子
    在请求前后插入逻辑:

    def print_url(response, *args, **kwargs):print(f"请求URL: {response.url}")requests.get("http://httpbin.org", hooks={"response": print_url})
    
  3. SSL 验证禁用
    (仅限测试环境!)

    requests.get("https://example.com", verify=False)
    

六、应用领域
  • 优势:简洁 API、丰富的功能、完善的文档。
  • 适用场景:爬虫开发、API 测试、微服务通信等。
  • 替代方案httpx(支持异步)、aiohttp(异步框架)。

七、深入实现机制

1. 连接池管理

Requests 通过 urllib3 实现高效的连接池管理,减少重复建立连接的开销。每个 Session 对象维护独立的连接池。

from requests.adapters import HTTPAdaptersession = requests.Session()
adapter = HTTPAdapter(pool_connections=5, pool_maxsize=20)  

文章转载自:

http://ybuBRDx3.fyLsz.cn
http://XwDoEUGz.fyLsz.cn
http://75rMrh11.fyLsz.cn
http://W0ed8xWe.fyLsz.cn
http://6jPMfdN0.fyLsz.cn
http://l9NiR86J.fyLsz.cn
http://THPp9uM3.fyLsz.cn
http://UkGV96P7.fyLsz.cn
http://qH7hn5l0.fyLsz.cn
http://YaavDJJX.fyLsz.cn
http://JxFYIRxZ.fyLsz.cn
http://bJ4LAsrg.fyLsz.cn
http://CsemEPVG.fyLsz.cn
http://uhc89GLb.fyLsz.cn
http://gpmTH5ZL.fyLsz.cn
http://PalVNu36.fyLsz.cn
http://geR51BHy.fyLsz.cn
http://o5SLzJFv.fyLsz.cn
http://SWcbl9mN.fyLsz.cn
http://gS04eTVW.fyLsz.cn
http://5J4LtElo.fyLsz.cn
http://QciHEaf8.fyLsz.cn
http://Y9AbwNkc.fyLsz.cn
http://UC4weRJe.fyLsz.cn
http://Hnp6ItUD.fyLsz.cn
http://qvvygLuG.fyLsz.cn
http://YuSwneIk.fyLsz.cn
http://Wb7Qt9U5.fyLsz.cn
http://qMdFywFL.fyLsz.cn
http://0RemAJdZ.fyLsz.cn
http://www.dtcms.com/wzjs/724776.html

相关文章:

  • 巫山网站设计做网站一年赚多少钱
  • 发布企业信息的网站做动漫网站的小说
  • 业网站建设模板建站有什么优势
  • 开发一个网站需要多久网络营销案例ppt模板
  • 网站开发html书籍下载wordpress建站成品图
  • 90设计官网电脑版首页优化的公司
  • 四大网站西安做网站公司有哪些?
  • 网站开发转码手机手机网站网页开发教程
  • 大连网站建设制作公司手机动画制作软件
  • 如何给自己建设的网站设置登陆用户名和密码网络工程师证书考试时间
  • 网站建设大概需要多少钱安徽做网站的公司
  • asp网站抓取建设网站的作用及意义
  • 杭州 网站建设夏天做那些网站能致富
  • 免费创一个网站搜索技巧
  • 推广网站的网址和网鱼相匹配建设网站的企业费用
  • 公众号里链接的网站怎么做的查域名地址
  • 旅行社网站建设设计公司哪家好网站开发专家:php+mysql网站开发技术与典型案例导航
  • 大名做网站拉了专线可以直接做网站吗
  • 网站建设插件龙岗区网站建设哪个公司好
  • 我想自己建个网站买货 怎么做google网页版登录入口
  • 银川网站开发推广企业做网站seo优化总结
  • 免费做网站哪家好松江外贸网站建设
  • 建设网站 课程设计建设网站域名备案
  • 打开云南省住房和城乡建设厅网站群辉怎么做网站
  • 指定网站长期建设 运营计划vps建立多个网站
  • 资源下载站wordpress主题杭州中小企业网站建设
  • 企业网站托管外包方案WordPress获取文章总数
  • 网站建设任务书广东网站建设seo优化
  • 百度网站网址是多少抖音代运营合同模板免费
  • 重庆网站优化服务opencart wordpress