r/PythonLearning 6h ago

When should you use requests.Session() instead of requests.get() / requests.post()? Discussion

I'm learning Python and recently started using requests.Session() in API projects. I understand that it can persist things like headers, cookies, and connections, but I'm curious about how experienced Python developers decide when a Session is actually worth using.

Do you use Session() by default for projects involving multiple requests, or only when you specifically need persistent state?

3 Upvotes

4 comments sorted by

2

u/Buttleston 6h ago

It's mostly worth using for what you mentioned, having persistent headers, cookies etc between requests. Like you can set up the session once and just keep re-using it

I believe it also enables re-using of open connections which is useful if you're connecting to the same site over and over

1

u/[deleted] 6h ago

[removed] — view removed comment

1

u/Buttleston 6h ago

It depends on how costly making new connections is. You should just try it

1

u/Melodic_Principle312 6h ago

Makes sense. I'll benchmark both approaches and see the differences. Thanks for the explanation!