5 篇文章带有标签 “http”

MCP 基础协议

模型上下文协议(Model Context Protocol)由几个协同工作的关键组件组成:

  • 基础协议:核心 JSON-RPC 消息类型
  • 生命周期管理:连接初始化、能力协商和会话控制
  • 服务器功能:服务器提供的资源、提示和工具
  • 客户端功能:客户端提供的采样和根目录列表
  • 实用工具:跨领域关注点,如日志记录和参数补全

所有实现必须支持基础协议和生命周期管理组件。其他组件可以根据应用程序的特定需求来实现。

这些协议层在实现客户端和服务器之间丰富交互的同时,建立了明确的关注点分离。模块化设计允许实现精确支持所需的功能。

消息

MCP 客户端和服务器之间的所有消息必须遵循 JSON-RPC 2.0 规范。协议定义了以下类型的消息:

请求(Requests)

请求从客户端发送到服务器,或者从服务器发送到客户端,用于启动操作。

{
  jsonrpc: "2.0";
  id: string | number;
  method: string;
  params?: {
    [key: string]: unknown;
  };
}
  • 请求必须包含字符串或整数 ID。
  • 与基本 JSON-RPC 不同,ID 不得null
  • 请求 ID 不得在同一会话中被请求者先前使用过。

响应(Responses)

响应是对请求的回复,包含操作的结果或错误。

命令curl

下载文件

下载单个文件

curl http://book.d2l.ai/_images/catdog.jpg -o catdog.jpg

下载多个文件

curl https://download.01.org/opencv/2021/openvinotoolkit/2021.1/open_model_zoo/models_bin/1/face-detection-retail-0004/FP32/face-detection-retail-0004.xml https://download.01.org/opencv/2021/openvinotoolkit/2021.1/open_model_zoo/models_bin/1/face-detection-retail-0004/FP32/face-detection-retail-0004.bin -o face-detection-retail-0004.xml -o face-detection-retail-0004.bin

下载文件并创建目录 curl --create-dirs https://download.01.org/opencv/2021/openvinotoolkit/2021.

HTTP 基准测试工具

wrk

wrk 使用的是 HTTP/1.1

安装

需要从 GitHub 上克隆代码自己编译,编译前需要安装 git, gcc。

git clone https://github.com/wg/wrk.git
cd wrk
#使用多线程(机器的处理器核数)加速编译,
make -j $(nproc)
cp wrk /usr/local/bin/

测试

10 个线程,保持打开 100 个并发连接,持续 10 秒。

wrk -t10 -c100 -d10 http://www.baidu.com/
Running 10s test @ http://www.baidu.com/
  10 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   138.46ms  219.05ms   1.89s    92.11%
    Req/Sec   128.07     79.83   700.00     94.40%
  12776 requests in 10.02s, 128.22MB read
  Socket errors: connect 0, read 57, write 0, timeout 3
Requests/sec:   1275.30
Transfer/sec:     12.80MB

打印详细的延时分布信息 wrk -c10 -t4 --latenc

Python爬虫实践

HTTP Status Code

418 I'm a teapot

import requests

url = "https://movie.douban.com/cinema/later/beijing/"
page = requests.get(url)
print(page.status_code)
418

在头信息中加入 User-Agent 来解决。在 Safari 浏览器中通过选择菜单[开发]->[显示JavaScript控制台],然后选择[网络]->[文稿]->[标头],在内容里的[请求]节可以找到 User-Agent。 import requests url = "https://movie.douban.com/cinema/later/beijing/" headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15'} page = requests.get(url, headers=headers) print(page.

Apache HTTP Server实践

修改端口号

  • /etc/apache2/ports.conf
sudo nano /etc/apache2/ports.conf
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 8081
......
......
  • /etc/apache2/sites-enabled/000-default.conf
sudo nano /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:8081>
......
......
</VirtualHost>
  • 重启服务
sudo systemctl restart apache2

参考资料