When using a Session, it seems you need to provide the full URL each time, e.g.
session = requests.Session()
session.get('http://myserver/getstuff')
session.get('http://myserver/getstuff2')
This gets a little tedious. Is there a way to do something like:
session = requests.Session(url_base='http://myserver')
session.get('/getstuff')
session.get('/getstuff2')
This feature has been asked on the forums a few times 1, 2, 3. The preferred approach as documented here, is subclassing, as follows:
from requests import Session
from urllib.parse import urljoinclass LiveServerSession(Session):def __init__(self, base_url=None):super().__init__()self.base_url = base_urldef request(self, method, url, *args, **kwargs):joined_url = urljoin(self.base_url, url)return super().request(method, joined_url, *args, **kwargs)
You would use this simply as follows:
baseUrl = 'http://api.twitter.com'
with LiveServerSession(baseUrl) as s:resp = s.get('/1/statuses/home_timeline.json')