This function was set on the forums several times 1 , 2 , 3 . The preferred approach described here is to subclass as follows:
from requests import Session from urlparse import urljoin class LiveServerSession(Session): def __init__(self, prefix_url=None, *args, **kwargs): super(LiveServerSession, self).__init__(*args, **kwargs) self.prefix_url = prefix_url def request(self, method, url, *args, **kwargs): url = urljoin(self.prefix_url, url) return super(LiveServerSession, self).request(method, 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')
source share