urllib2/requests and HTTP relative path
Question: How can I force urllib2/requests modules to use relative paths instead of
full/absolute URL??

when I send request using urllib2/requests I see in my proxy that it resolves
it to:

    
    
    GET https://xxxx/path/to/something HTTP/1.1
    

Unfortunately, the server to which I'm sending it, cannot understand that
request and gives me weird 302. I know it's in RFC, it just doesn't work and
I'm tryign to fix it in python code. I don't have access to that server.

Relative path, works well

    
    
    GET /path/to/something HTTP/1.1
    Host: xxxx
    

So how can I force requests/urllib2 to not use absolute paths? and use simple
relative paths?


Answer: Probably the following code would do for your case:

    
    
    from urlparse import urljoin
    import requests
    
    class RelativeSession(requests.Session):
        def __init__(self, base_url):
            super(RelativeSession, self).__init__()
            self.__base_url = base_url
    
        def request(self, method, url, **kwargs): 
            url = urljoin(self.__base_url, url)
            return super(RelativeSession, self).request(method, url, **kwargs)
    
    session = RelativeSession('http://server.net')
    response = session.get('/rel/url')
    

