【发布时间】:2022-07-02 15:28:43
【问题描述】:
问题
我需要同时执行 HTTP 请求并模拟高延迟。我在 Python 中遇到了 Twisted 包,其中包括 HTTP client 和 ThrottlingFactory。我遇到的问题是对于新手来说文档不清楚,而且我无法理解如何使用 HTTP 客户端在 API 调用中利用 ThrottlingFactory。
我目前正在使用以下示例代码进行测试。到目前为止没有任何效果。
from sys import argv
from pprint import pformat
from twisted.internet.task import react
from twisted.web.client import Agent, readBody
from twisted.web.http_headers import Headers
def cbRequest(response):
print("Response version:", response.version)
print("Response code:", response.code)
print("Response phrase:", response.phrase)
print("Response headers:")
print(pformat(list(response.headers.getAllRawHeaders())))
d = readBody(response)
d.addCallback(cbBody)
return d
def cbBody(body):
print("Response body:")
print(body)
def main(reactor, url=b"http://httpbin.org/get"):
agent = Agent(reactor)
d = agent.request(
b"GET", url, Headers({"User-Agent": ["Twisted Web Client Example"]}), None
)
d.addCallback(cbRequest)
return d
react(main, argv[1:])
如何在本例中使用 ThrottlingFactory?
【问题讨论】:
标签: python twisted bandwidth-throttling