urllib2/requests and HTTP relative path

前端 未结 2 1959
迷失自我
迷失自我 2021-01-14 05:06

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 re

相关标签:
2条回答
  • 2021-01-14 05:23

    I think there is a little bit of confusion here. As per RFC 2616 only absolute path or absolute URI are allowed in http request line. There is simply no such thing as relative http request -- as basicly http is stateless.

    In your question you talking about proxy, that RFC state clearly that:

    The absoluteURI form is REQUIRED when the request is being made to a proxy.

    As per se, AFAIK, your proxy is not HTTP/1.1 compliant. Is this a commercial product or an in-house development?

    By the way, HTTP 302 is a redirect. Are you sure the ressource hasn't simply moved to an other location?


    Anyway, by looking at the source code or requests (requests/models.py L276) I'm afraid it doesn't seem to have any easy way to force the use of absolute path

    My best bet would be to change the PreparedRequest object before it is send as described in advanced usages/Prepared request.

    0 讨论(0)
  • 2021-01-14 05:37

    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')
    
    0 讨论(0)
提交回复
热议问题