当前位置: 首页>>代码示例>>Python>>正文


Python URLObject.with_netloc方法代码示例

本文整理汇总了Python中urlobject.URLObject.with_netloc方法的典型用法代码示例。如果您正苦于以下问题:Python URLObject.with_netloc方法的具体用法?Python URLObject.with_netloc怎么用?Python URLObject.with_netloc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在urlobject.URLObject的用法示例。


在下文中一共展示了URLObject.with_netloc方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_page

# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_netloc [as 别名]
    def get_page(request):
        """
        reverse proxy Django view
        """
        request_url = URLObject(request.build_absolute_uri())
        origin_url = URLObject(origin_server)
        # if origin server is not a url then assume it's netloc? to make it compat with previous version
        target_url = request_url.with_netloc(origin_server)
        if origin_url.scheme:
            target_url = request_url.with_netloc(origin_url.netloc).with_scheme(origin_url.scheme)

        # Construct headers
        headers = {}
        for header, value in request.META.items():
            if header.startswith('HTTP_') or header in ['CONTENT_TYPE', 'CONTENT_LENGTH']:
                name = header.replace('HTTP_', '').replace('_', '-').title()

                # Not forward empty content-length (esp in get), this causes weird response
                if name.lower() == 'content-length' and value == '':
                    continue

                # An HTTP/1.1 proxy MUST ensure that any request message it forwards does contain an appropriate
                # Host header field that identifies the service being requested by the proxy.
                if name.lower() == 'host':
                    if origin_url.scheme == '':
                        value = origin_server
                    else:
                        value = str(URLObject(origin_server).netloc)

                # Assigning headers' values
                if name.lower() not in _hop_headers.keys():
                    headers[name] = value

        # Send request
        http = Http(**_httplib2_constructor_kwargs)
        http.follow_redirects = False
        if hasattr(request, 'body'):
            request_body = request.body
        else:
            request_body = request.raw_post_data
        httplib2_response, content = http.request(
            target_url, request.method,
            body=bytearray(request_body),
            headers=headers)

        # Construct Django HttpResponse
        content_type = httplib2_response.get('content-type', DEFAULT_CONTENT_TYPE)
        response = HttpResponse(content, status=httplib2_response.status, content_type=content_type)

        update_response_headers(response, httplib2_response)
        update_messages_cookie(request, headers, httplib2_response, response)

        if httplib2_response.status in [301, 302]:
            location_url = URLObject(httplib2_response['location'])
            if origin_url.scheme == '':
                response['location'] = location_url.with_netloc(request.get_host())
            else:
                response['location'] = location_url.with_netloc(request_url.netloc).with_scheme(request_url.scheme)
        return response
开发者ID:Proteus-tech,项目名称:django-roxy,代码行数:61,代码来源:views.py

示例2: proxied

# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_netloc [as 别名]
def proxied(url):
    url = URLObject(url)
    netloc = url.netloc or settings.SERVER_NAME
    cache = get_cache()
    if netloc not in cache:
        return url
    return url.with_netloc(cache[netloc])
开发者ID:paulvisen,项目名称:flask-todo,代码行数:9,代码来源:cdn.py

示例3: URLObjectModificationTest

# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_netloc [as 别名]
class URLObjectModificationTest(unittest.TestCase):

    def setUp(self):
        self.url = URLObject('https://github.com/zacharyvoase/urlobject?spam=eggs#foo')

    def test_with_scheme_replaces_scheme(self):
        assert (self.url.with_scheme('http') ==
                'http://github.com/zacharyvoase/urlobject?spam=eggs#foo')

    def test_with_netloc_replaces_netloc(self):
        assert (self.url.with_netloc('example.com') ==
                'https://example.com/zacharyvoase/urlobject?spam=eggs#foo')

    def test_with_hostname_replaces_hostname(self):
        url = URLObject('https://user:[email protected]/')
        assert (url.with_hostname('example.com') ==
                'https://user:[email protected]/')

    def test_with_username_adds_username(self):
        url = URLObject('https://github.com/')
        assert url.with_username('zack') == 'https://[email protected]/'

    def test_with_username_replaces_username(self):
        url = URLObject('https://[email protected]/')
        assert url.with_username('alice') == 'https://[email protected]/'

    def test_without_username_removes_username(self):
        url = URLObject('https://[email protected]/')
        assert url.without_username() == 'https://github.com/'

    def test_with_password_adds_password(self):
        url = URLObject('https://[email protected]/')
        assert url.with_password('1234') == 'https://zack:[email protected]/'

    def test_with_password_raises_ValueError_when_there_is_no_username(self):
        url = URLObject('https://github.com/')
        assert_raises(ValueError, lambda: url.with_password('1234'))

    def test_with_password_replaces_password(self):
        url = URLObject('https://zack:[email protected]/')
        assert url.with_password('5678') == 'https://zack:[email protected]/'

    def test_without_password_removes_password(self):
        url = URLObject('https://zack:[email protected]/')
        assert url.without_password() == 'https://[email protected]/'

    def test_with_auth_with_one_arg_adds_username(self):
        url = URLObject('https://github.com/')
        assert url.with_auth('zack') == 'https://[email protected]/'

    def test_with_auth_with_one_arg_replaces_whole_auth_string_with_username(self):
        url = URLObject('https://alice:[email protected]/')
        assert url.with_auth('zack') == 'https://[email protected]/'

    def test_with_auth_with_two_args_adds_username_and_password(self):
        url = URLObject('https://github.com/')
        assert url.with_auth('zack', '1234') == 'https://zack:[email protected]/'

    def test_with_auth_with_two_args_replaces_whole_auth_string_with_username_and_password(self):
        # Replaces username-only auth string
        url = URLObject('https://[email protected]/')
        assert url.with_auth('zack', '1234') == 'https://zack:[email protected]/'

        # Replaces username and password.
        url = URLObject('https://alice:[email protected]/')
        assert url.with_auth('zack', '1234') == 'https://zack:[email protected]/'

    def test_without_auth_removes_entire_auth_string(self):
        # No username or password => no-op.
        url = URLObject('https://github.com/')
        assert url.without_auth() == 'https://github.com/'
        # Username-only.
        url = URLObject('https://[email protected]/')
        assert url.without_auth() == 'https://github.com/'
        # Username and password.
        url = URLObject('https://alice:[email protected]/')
        assert url.without_auth() == 'https://github.com/'

    def test_with_port_adds_port_number(self):
        assert (self.url.with_port(24) ==
                'https://github.com:24/zacharyvoase/urlobject?spam=eggs#foo')

    def test_with_port_replaces_port_number(self):
        url = URLObject('https://github.com:59/')
        assert url.with_port(67) == 'https://github.com:67/'

    def test_without_port_removes_port_number(self):
        url = URLObject('https://github.com:59/')
        assert url.without_port() == 'https://github.com/'

    def test_with_path_replaces_path(self):
        assert (self.url.with_path('/dvxhouse/intessa') ==
                'https://github.com/dvxhouse/intessa?spam=eggs#foo')

    def test_root_goes_to_root_path(self):
        assert self.url.root == 'https://github.com/?spam=eggs#foo'

    def test_parent_jumps_up_one_level(self):
        url = URLObject('https://github.com/zacharyvoase/urlobject')
        assert url.parent == 'https://github.com/zacharyvoase/'
#.........这里部分代码省略.........
开发者ID:vmalloc,项目名称:urlobject,代码行数:103,代码来源:urlobject_test.py


注:本文中的urlobject.URLObject.with_netloc方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。