urllib2/requests and HTTP relative path

断了今生、忘了曾经 提交于 2019-12-01 06:37:33

问题


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?


回答1:


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')



回答2:


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.



来源:https://stackoverflow.com/questions/17045550/urllib2-requests-and-http-relative-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!