Browse Source

SteamClient: cache session from get_web_session()

* the requests.Session object is cached internally, until the instance disconnects
* added steam.util.generate_session_id() for web session
pull/35/merge
Rossen Georgiev 8 years ago
parent
commit
9b3cff7f8a
  1. 16
      steam/client/builtins/web.py
  2. 9
      steam/util/web.py

16
steam/client/builtins/web.py

@ -5,13 +5,20 @@ from steam import webapi
from steam.core.msg import MsgProto
from steam.enums.emsg import EMsg
from steam.core.crypto import generate_session_key, symmetric_encrypt
from steam.util.web import make_requests_session
from steam.util.web import make_requests_session, generate_session_id
class Web(object):
_web_session = None
def __init__(self, *args, **kwargs):
super(Web, self).__init__(*args, **kwargs)
self.on(self.EVENT_DISCONNECTED, self.__handle_disconnect)
def __handle_disconnect(self):
self._web_session = None
def get_web_session_cookies(self):
"""Get web authentication cookies via WebAPI's ``AuthenticateUser``
@ -62,11 +69,15 @@ class Web(object):
:return: authenticated Session ready for use
:rtype: :class:`requests.Session`, :class:`None`
"""
if self._web_session:
return self._web_session
cookies = self.get_web_session_cookies()
if cookies is None:
return None
session = make_requests_session()
self._web_session = session = make_requests_session()
session_id = generate_session_id()
for domain in ['store.steampowered.com', 'help.steampowered.com', 'steamcommunity.com']:
for name, val in cookies.items():
@ -75,5 +86,6 @@ class Web(object):
session.cookies.set('Steam_Language', language, domain=domain)
session.cookies.set('birthtime', '-3333', domain=domain)
session.cookies.set('sessionid', session_id, domain=domain)
return session

9
steam/util/web.py

@ -1,4 +1,6 @@
import requests
from binascii import hexlify
from steam.core.crypto import sha1_hash, random_bytes
def make_requests_session():
"""
@ -13,3 +15,10 @@ def make_requests_session():
session.headers['User-Agent'] = ua
return session
def generate_session_id():
"""
:returns: session id
:rtype: :class:`str`
"""
return hexlify(sha1_hash(random_bytes(32)))[:32]

Loading…
Cancel
Save