HTTP代理记录和重放请求
x-man的Python项目详细描述
杰曼
Xman
是一个记录和重放请求的HTTP代理。
它充当可扩展的“中间人”服务器,它可以:
- 将请求转发到其他地址
- 立即返回缓存结果,无需代理
- 从其中记录传入的响应
- 动态转换请求和响应(例如,用正则表达式替换路径)
- 当客户端请求太频繁时限制请求
使用xman
可以设置模拟服务器,模拟真实服务器:
- 将其配置为转发到实际服务器。启用录制请求和重放响应。在
- 提出一些典型的要求。请求-响应条目将被记录到一个文件中。在
- 现在可以关闭真正的服务器了。从缓存返回响应。在
- 将
xman
与记录的数据一起使用,以便在任何地方设置轻量级HTTP服务模拟。在
安装
pip3 install x-man
需要Python3.6(或更新版本)。在
快速启动
配置侦听SSL端口8443,使用缓存将请求转发到https://127.0.0.1:8000。 当相同的请求出现时,将返回缓存的响应。在
^{pr2}$运行docker
您可以在docker中运行xman
,并在末尾传递自定义参数。
该命令只打印出帮助:
docker run --rm -it --network=host igrek5151/xman:latest
基本转发带有基本缓存的所有请求:
docker run --rm -it --network=host igrek5151/xman:latest \ http://127.0.0.1:8000 --listen-port 8443 --listen-ssl=true --replay=true
要进行更多自定义,请创建您自己的ext.py
扩展文件(下面一节中的示例)并运行:
docker run --rm -it --network=host -v `pwd`/ext.py:/ext.py igrek5151/xman:latest \ --config=/ext.py
如果要将记录的请求和响应保留在容器外部,请同时装入tape.json
:
touch tape.json docker run --rm -it --network=host -v `pwd`/ext.py:/ext.py -v `pwd`/tape.json:/src/tape.json igrek5151/xman:latest \ --config=/ext.py --record=true --replay=true
扩展
如果您需要更多的定制,您可以指定扩展文件,您可以在其中实现您的自定义行为甚至处理逻辑。
为此,必须创建Python脚本并通过参数传递其文件名:xman --config ext.py
。在
在扩展文件中,您可以指定请求/响应映射器或自定义比较器来决定哪些请求应该被视为相同的。使用它可以为某些特定类型的请求实现自定义行为。在
执行您的功能以代替以下功能之一:
transform_request(request: HttpRequest) -> HttpRequest
-在进一步处理(缓存、转发)之前转换每个传入的请求。在transform_response(request: HttpRequest, response: HttpResponse) -> HttpResponse
-在发送之前转换每个响应。在immediate_responder(request: HttpRequest) -> Optional[HttpResponse]
-返回匹配请求的即时响应,而不是进一步代理请求或在缓存中搜索can_be_cached(request: HttpRequest, response: HttpResponse) -> bool
-指示带有响应的特定请求是否可以保存在缓存中。在cache_request_traits(request: HttpRequest) -> Tuple
-获取表示请求唯一性的元组。缓存时,具有相同结果的请求将被视为相同的。在override_config(config: Config)
-覆盖配置中的默认参数。在
扩展示例
外景py
fromtypingimportTuple,Optionalfromnuclear.sublogimportlogfromxman.cacheimportsorted_dict_traitfromxman.configimportConfigfromxman.requestimportHttpRequestfromxman.responseimportHttpResponsefromxman.transformimportreplace_request_pathdeftransform_request(request:HttpRequest)->HttpRequest:"""Transforms each incoming Request before further processing (caching, forwarding)."""returnreplace_request_path(request,r'^/some/path/(.+?)(/[a-z]+)(/.*)',r'\3')defimmediate_responder(request:HttpRequest)->Optional[HttpResponse]:"""Returns immediate response for matched request instead of proxying it further or searching in cache"""ifrequest.path.startswith('/some/api'):returnHttpResponse(status_code=200,headers={'Content-Type':'application/json'},content=''.encode())returnNonedeftransform_response(request:HttpRequest,response:HttpResponse)->HttpResponse:"""Transforms each Response before sending it."""ifrequest.path.startswith('/some/api'):log.debug('Found Ya',path=request.path)response=response.set_content('{"payload": "anythingyouwish"}"')returnresponsedefcan_be_cached(request:HttpRequest,response:HttpResponse)->bool:"""Indicates whether particular request with response could be saved in cache."""returnresponse.status_code==200defcache_request_traits(request:HttpRequest)->Tuple:"""Gets tuple denoting request uniqueness. Requests with same results are treated as the same when caching."""ifrequest.path.endswith('/some/path'):returnrequest.method,request.path,sorted_dict_trait(request.headers)returnrequest.method,request.path,request.contentdefoverride_config(config:Config):"""Overrides default parameters in config."""# config.listen_port = 8080# config.listen_ssl = True# config.dst_url = 'http://127.0.0.1:8000'# config.record = False# config.record_file = 'tape.json'# config.replay = False# config.replay_throttle = False# config.replay_clear_cache = False# config.replay_clear_cache_seconds = 60# config.allow_chunking = True# config.proxy_timeout = 10config.verbose=0
使用
键入xman
查看帮助:
xman v0.1.2 (nuclear v1.1.9) - HTTP proxy recording & replaying requestsUsage:xman [OPTIONS] [DST_URL]Arguments: [DST_URL] - destination base url Default: http://127.0.0.1:8000Options: --version - Print version information and exit -h, --help [SUBCOMMANDS...] - Display this help and exit --listen-port LISTEN_PORT - listen port for incoming requests Default: 8080 --listen-ssl LISTEN_SSL - enable https on listening side Default: True --record RECORD - enable recording requests & responses Default: False --record-file RECORD_FILE - filename with recorded requests Default: tape.json --replay REPLAY - return cached results if found Default: False --replay-throttle REPLAY_THROTTLE - throttle response if too many requests are made Default: False --replay-clear-cache REPLAY_CLEAR_CACHE - enable clearing cache periodically Default: False --replay-clear-cache-seconds REPLAY_CLEAR_CACHE_SECONDS - clearing cache interval in seconds Default: 60 --allow-chunking ALLOW_CHUNKING - enable sending response in chunks Default: True --config CONFIG - load extensions from Python file -v, --verbose - show more details in output
- 项目
标签: