Skip to main content

Python library for GoReplay Middleware

Project description

Python library for GoReplay Middleware , API is quite similar to NodeJS library

https://badge.fury.io/py/gor.svg https://travis-ci.org/amyangfei/GorMW.svg?branch=master

Installation

To install GorMW, simply:

$ pip install gor

or from source:

python setup.py install

Getting Started

Initialize a TornadoGor based middleware and start it in the following way:

from gor.middleware import TornadoGor
proxy = TornadoGor()
proxy.run()

Basic idea is that you write callbacks which respond to request, response, replay, or message events, which contains request meta information and actuall http paylod. Depending on your needs you may compare, override or filter incoming requests and responses.

You can respond to the incoming events using on function, by providing callbacks:

def on_request(proxy, msg, **kwargs):
    # do anything you want with msg
    # msg is a GorMessage object
    pass

proxy = TornadoGor()
proxy.on('request', on_request)
proxy.run()

You can provide request ID as additional argument to on function, which allow you to map related requests and responses. Below is example of middleware which checks that original and replayed response have same HTTP status code.

# coding: utf-8
import sys
from gor.middleware import TornadoGor


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 = TornadoGor()
    proxy.on('request', on_request)
    proxy.run()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gor-0.1.2.tar.gz (4.6 kB view details)

Uploaded Source

File details

Details for the file gor-0.1.2.tar.gz.

File metadata

  • Download URL: gor-0.1.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for gor-0.1.2.tar.gz
Algorithm Hash digest
SHA256 eae8a2f31fd43386febfb0ebc4d5e77269e9258ddce0344709288a639f8ab327
MD5 39212d41cf920b0cd9be49df233796c9
BLAKE2b-256 ff6c2a92e4cee19a705710a816fc5e50f5250e9d30c9d9b0788e4b0e490168aa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page