http post协议实现简单的rpc协议,WireShark抓包分析

文章目录

1.http 客户端-RPC客户端

python 复制代码
import json
import requests

# 定义 RPC 客户端类
class RPCClient:
    def __init__(self, server_url):
        self.server_url = server_url

    def call(self, method, params):
        # 构建 JSON-RPC 请求
        request_data = {
            'method': method,
            'params': params,
            'id': 1
        }
        #json数据转化为网络字节流数据
        json_data = json.dumps(request_data).encode('utf-8')

        # 发送 POST 请求
        response = requests.post(self.server_url, json_data, headers={'Content-type': 'application/json'})

        # 解析响应
        result = response.json()
        if 'error' in result:
            raise Exception(result['error'])
        return result['result']

# 创建 RPC 客户端实例
client = RPCClient('http://192.168.1.9:8000')

# 调用远程方法
result = client.call('add', [3, 5])
print("Result:", result)

1.http 服务端-RPC服务端

python 复制代码
import json
from http.server import BaseHTTPRequestHandler, HTTPServer

# 自定义处理程序类
class RPCHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        # 解析接收到的 JSON 数据
        content_length = int(self.headers['Content-Length'])
        json_data = self.rfile.read(content_length).decode('utf-8')
        print("-------json_data:{}".format(json_data))
        data = json.loads(json_data)
        print("-------data:{}".format(data))
        # 处理 RPC 请求
        result = self.process_request(data)

        # 构建响应
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()
        # 将python对象转为json字符串,encode用于将字符串转换为字节流b开头,默认用utf-8编码
        print(json.dumps(result).encode('utf-8'))
        self.wfile.write(json.dumps(result).encode('utf-8'))

    def process_request(self, data):
        # 在这里处理 RPC 请求
        # 根据请求内容调用相应的函数或方法
        # 返回结果
        if 'method' in data:
            method = data['method']
            if method == 'add':
                if 'params' in data and len(data['params']) == 2:
                    a, b = data['params']
                    result = a + b
                    return {'result': result}
        return {'error': 'Invalid request'}

# 启动 RPC 服务器
def run_server():
    port = 8000
    server_address = ('192.168.1.9', port)
    httpd = HTTPServer(server_address, RPCHandler)
    httpd.serve_forever()

if __name__ == '__main__':
    run_server()

3.WireShark抓包分析

3.1客户端到服务端的HTTP/JSON报文

3.2服务端到客户端的HTTP/JSON报文

相关推荐
cfm_29143 小时前
初步了解ECDHE密钥交换
http
Lsetea5 小时前
curl -I检查HTTPS返回405:HEAD、GET与证书验收分层排查
linux·http·https·ssl证书·curl
我命由我123455 小时前
Python 开发 - Python 虚拟环境
运维·开发语言·后端·python·http·https·java-ee
紧跟先前的步伐8 小时前
Go AI Agent 实战 08|HTTP 模型 API 的工作原理与请求流程
人工智能·http·ai·golang
大泽九章11 小时前
HTTP协议安全入门:从请求走私到CRLF
网络协议·安全·http
肖永威12 小时前
python版微电网仿真工具的前端托管选型:从http.server到FastAPI StaticFiles
前端·python·http·fastapi
AbandonForce12 小时前
Linux 网络:HTTP(一)基础与报文结构
linux·网络·http
SatanII1 天前
K8s 存储与配置管理|Volume、ConfigMap、Secret 核心笔记
笔记·rpc·kubernetes
严谨的麻辣烫1 天前
静态代理 IP 的出口一致性如何验证?Python 请求检测与异常排查
开发语言·python·http·网络安全
川石课堂软件测试1 天前
涨薪技术|0到1学会性能测试第9课-HTTP响应统计
数据库·功能测试·网络协议·http·kubernetes·单元测试·测试用例