Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests: URL base in Session

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') 
like image 810
paj28 Avatar asked Mar 04 '17 21:03

paj28


People also ask

How do I find the base URL in python?

Pass the url to the urlparse method from the urllib. parse module. Access the netloc attribute on the parse result.

What is URL in request in Python?

url returns the URL of the response. It will show the main url which has returned the content, after all redirections, if done. Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object.

What is Python requests Session?

Usind Session object with Python Requests. The Requests Session object allows you to persist specific parameters across requests to the same site. To get the Session object in Python Requests, you need to call the requests. Session() method. The Session object can store such parameters as cookies and HTTP headers.

What is stream true in Python?

1 Answer. Show activity on this post. using stream = True sets the stage for you to read the response data in chunks as opposed to having the entire response body downloaded in one go upfront.


2 Answers

requests_toolbelt.sessions.BaseUrlSession https://github.com/requests/toolbelt/blob/f5c86c51e0a01fbc8b3b4e1c286fd5c7cb3aacfa/requests_toolbelt/sessions.py#L6

NOTE: This uses urljoin from standard lib. Beware of urljoin's behavior.

In [14]: from urlparse import urljoin  In [15]: urljoin('https://localhost/api', '/resource') Out[15]: 'https://localhost/resource'  In [16]: urljoin('https://localhost/api', 'resource') Out[16]: 'https://localhost/resource'  In [17]: urljoin('https://localhost/api/', '/resource') Out[17]: 'https://localhost/resource'  In [18]: urljoin('https://localhost/api/', 'resource') Out[18]: 'https://localhost/api/resource' 

OR

import requests  from functools import partial  def PrefixUrlSession(prefix=None):                                                                                                                                                                                                                                                                                                                       if prefix is None:                                                                                                                                                                                                                                                                                                                                       prefix = ""                                                                                                                                                                                                                                                                                                                                      else:                                                                                                                                                                                                                                                                                                                                                    prefix = prefix.rstrip('/') + '/'                                                                                                                                                                                                                                                                                                                 def new_request(prefix, f, method, url, *args, **kwargs):                                                                                                                                                                                                                                                                                                return f(method, prefix + url, *args, **kwargs)                                                                                                                                                                                                                                                                                                   s = requests.Session()                                                                                                                                                                                                                                                                                                                               s.request = partial(new_request, prefix, s.request)                                                                                                                                                                                                                                                                                                  return s              
like image 34
khluasdfikuljasdlkfasd Avatar answered Sep 21 '22 21:09

khluasdfikuljasdlkfasd


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 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') 
like image 56
rouble Avatar answered Sep 23 '22 21:09

rouble