Keep python request session in persistent storage

I am using python query library. My application fulfills a simple request to receive from the site and displays the results.

The site requires authorization using ntlm. Fortunately, I can rely on HttpNtlmAuth, which works great.

session = requests.Session() session.auth = HttpNtlmAuth(domain + "\\" + username, password, session) 

But if the application is executed several times - each time I need to set a username and password. It is very uncomfortable. Saving credentials is undesirable.

Can I save a session object myself and reuse it several times? From a server perspective, this should be fine.

Is there a way to sort and decompress a session?

+5
source share
1 answer

If you use the dill package, you should be able to sort the session where pickle itself does not work.

 >>> import dill as pickle >>> pickled = pickle.dumps(session) >>> restored = pickle.loads(pickled) 

Get dill here: https://github.com/uqfoundation/dill

In fact, dill also makes it easy to save your python session through reboots, so you could pickle complete your entire python session as follows:

 >>> pickle.dump_session('session.pkl') 

Then restart python and pick up where you left off.

 Python 2.7.8 (default, Jul 13 2014, 02:29:54) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import dill as pickle >>> pickle.load_session('session.pkl') >>> restored <requests.sessions.Session object at 0x10c012690> 
+6
source

Source: https://habr.com/ru/post/1209354/


All Articles