代码之家  ›  专栏  ›  技术社区  ›  systempuntoout

如何在Python中测试webapi限制

  •  1
  • systempuntoout  · 技术社区  · 14 年前

    此API限制允许每个IP最多X个请求超过Y秒。

    你能提出一个好办法吗?

    1 回复  |  直到 14 年前
        1
  •  2
  •   Gintautas Miliauskas    14 年前

    我将编写一个脚本来执行以下操作:

    1. 发出X个请求,对每个请求计时(我将使用 time.time() ). 计时结果中应该没有节流的迹象。如果延迟显著,您可能需要并行化以达到限制。

    更新:以下是HTTP请求的示例代码:

    import time
    import urllib2
    
    URL = 'http://twitter.com'
    
    def request_time():
        start_time = time.time()
        urllib2.urlopen(URL).read()
        end_time = time.time()
        return end_time - start_time
    
    def throttling_test(n):
        """Test if processing more than n requests is throttled."""
        experiment_start = time.time()
        for i in range(n):
            t = request_time()
            print 'Request #%d took %.5f ms' % (i+1, t * 1000.0)
        print '--- Throttling limit crossed ---'
    
        t = request_time()
        print 'Request #%d took %.5f ms' % (n+1, t * 1000.0)
    
    
    throttling_test(3)