【goreplay】python简单使用goreplay中间件功能

一、场景

    流量录制,需要对播放的流量进程定制化处理,那么可以使用中间件来实现

 

 

二、官网

https://pypi.org/project/gor/

 

 

三、编写中间件代码

复制代码
# coding: utf-8
import sys
from gor.middleware import AsyncioGor


def on_request(proxy, msg, **kwargs):
    proxy.on('response', on_response, idx=msg.id, req=msg)

def on_response(proxy, msg, **kwargs):
    proxy.on('replay', on_replay, idx=kwargs['req'].id, req=kwargs['req'], resp=msg)

def on_replay(proxy, msg, **kwargs):
    replay_status = proxy.http_status(msg.http)
    resp_status = proxy.http_status(kwargs['resp'].http)
    if replay_status != resp_status:
        sys.stderr.write('replay status [%s] diffs from response status [%s]\n' % (replay_status, resp_status))
    else:
        sys.stderr.write('replay status is same as response status\n')
    sys.stderr.flush()

if __name__ == '__main__':
    proxy = AsyncioGor()
    proxy.on('request', on_request)
    proxy.run()
复制代码

有on_request, on_response, on_replay三个关键方法

 

四、使用

sudo ./gor --input-raw :8000 --middleware "python3 /path/to/middleware.py" --output-http-track-response --input-raw-track-response --output-http="http://192.168.3.123:8001"

可能会报错

 需要使用sudo安装gor库

$ sudo pip3 install gor
Collecting gor
  Downloading gor-0.2.3-py3-none-any.whl (6.9 kB)
Installing collected packages: gor
Successfully installed gor-0.2.3

 

五、我们查看效果

 

参考链接:

https://pypi.org/project/gor/

 

posted @   代码诠释的世界  阅读(68)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示