2

The document of Requests give some code like this

s = requests.Session()

s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get("http://httpbin.org/cookies")

If I want to use the connection-pooling feature of request.Session, Do I need to instantiate a new Session every time I send a request?, If I do not need a new session, how can I check whether the old seesion is valid now?

4

2 に答える 2

1

接続プーリングを使用する場合は、新しいセッションを作成したくありません。接続プーリングはオブジェクトによって管理されるため、Sessionすべてのリクエストをオブジェクトを介して渡す必要があります。

一般に、リクエストを使用してプログラムで 2 つまたは 3 つ以上の HTTP リクエストを実行する場合は、単一のSessionオブジェクトを作成し、それを通じてすべてのリクエストを実行する必要があります。これにより、Cookie の永続性や接続プーリングなど、Requests が提供する優れた機能がすべて提供されます。

于 2013-06-28T08:17:10.900 に答える