Python yarl.URL Examples

The following are 30 code examples of yarl.URL(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module yarl , or try the search function .
Example #1
Source File: test_connector.py    From aiosocks with Apache License 2.0 6 votes vote down vote up
def test_connect_remote_resolve(loop):
    tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')

    with mock.patch('aiosocks.connector.create_connection',
                    make_mocked_coro((tr, proto))):
        req = ProxyClientRequest(
            'GET', URL('http://python.org'), loop=loop,
            proxy=URL('socks5://127.0.0.1'))
        connector = ProxyConnector(loop=loop, remote_resolve=True)
        connector._resolve_host = make_mocked_coro([mock.MagicMock()])
        conn = await connector.connect(req, [], ClientTimeout())

    assert connector._resolve_host.call_count == 1
    assert conn.protocol is proto

    conn.close() 
Example #2
Source File: test_connector.py    From aiosocks with Apache License 2.0 6 votes vote down vote up
def test_connect_proxy_domain():
    tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')

    with mock.patch('aiosocks.connector.create_connection',
                    make_mocked_coro((tr, proto))):
        loop_mock = mock.Mock()

        req = ProxyClientRequest(
            'GET', URL('http://python.org'),  loop=loop_mock,
            proxy=URL('socks5://proxy.example'))
        connector = ProxyConnector(loop=loop_mock)

        connector._resolve_host = make_mocked_coro([mock.MagicMock()])
        conn = await connector.connect(req, [], ClientTimeout())

    assert connector._resolve_host.call_count == 1
    assert conn.protocol is proto

    conn.close() 
Example #3
Source File: resource.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_watcher_info(request: web.Request, agent_id: str) -> dict:
    '''
    Get watcher information.

    :return addr: address of agent watcher (eg: http://127.0.0.1:6009)
    :return token: agent watcher token ("insecure" if not set in config server)
    '''
    token = request.app['config']['watcher']['token']
    if token is None:
        token = 'insecure'
    agent_ip = await request.app['registry'].config_server.get(f'nodes/agents/{agent_id}/ip')
    watcher_port = await request.app['registry'].config_server.get(
        f'nodes/agents/{agent_id}/watcher_port')
    if watcher_port is None:
        watcher_port = 6009
    # TODO: watcher scheme is assumed to be http
    addr = yarl.URL(f'http://{agent_ip}:{watcher_port}')
    return {
        'addr': addr,
        'token': token,
    } 
Example #4
Source File: test_connector.py    From aiosocks with Apache License 2.0 6 votes vote down vote up
def test_connect_proxy_ip(loop):
    tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')

    with mock.patch('aiosocks.connector.create_connection',
                    make_mocked_coro((tr, proto))):
        loop.getaddrinfo = make_mocked_coro(
             [[0, 0, 0, 0, ['127.0.0.1', 1080]]])

        req = ProxyClientRequest(
            'GET', URL('http://python.org'), loop=loop,
            proxy=URL('socks5://proxy.org'))
        connector = ProxyConnector(loop=loop)
        conn = await connector.connect(req, [], ClientTimeout())

    assert loop.getaddrinfo.called
    assert conn.protocol is proto

    conn.close() 
Example #5
Source File: clicklist.py    From crocoite with MIT License 6 votes vote down vote up
def run(self):
        # XXX: do this once only
        fd = pkg_resources.resource_stream ('crocoite', 'data/click.yaml')
        config = list (yaml.safe_load_all (fd))

        l = nodes.definition_list ()
        for site in config:
            urls = set ()
            v = nodes.definition ()
            vl = nodes.bullet_list ()
            v += vl
            for s in site['selector']:
                i = nodes.list_item ()
                i += nodes.paragraph (text=s['description'])
                vl += i
                urls.update (map (lambda x: URL(x).with_path ('/'), s.get ('urls', [])))

            item = nodes.definition_list_item ()
            term = ', '.join (map (lambda x: x.host, urls)) if urls else site['match']
            k = nodes.term (text=term)
            item += k

            item += v
            l += item
        return [l] 
Example #6
Source File: fakehttp.py    From atomicpuppy with MIT License 6 votes vote down vote up
def registerEmptyUri(self, uri, status):
        def cb():
            fut = asyncio.Future(loop=self._loop)
            resp = ClientResponse(
                'GET',
                yarl.URL('foo'),
                writer=Mock(),
                timer=TimerNoop(),
                continue100=None,
                request_info=Mock(),
                traces=[],
                loop=self._loop,
                session=Mock()
                )
            resp.status = status

            # setting this as aiohttp 3.5.4 is now checking if this value is not None
            # see aiohttp/client_reqrep.py:934
            resp.reason = http.client.responses[status]

            fut.set_result(resp)
            return fut
        self._callbacks[uri].append(cb) 
Example #7
Source File: test_connector.py    From aiosocks with Apache License 2.0 6 votes vote down vote up
def test_connect_locale_resolve(loop):
    tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')

    with mock.patch('aiosocks.connector.create_connection',
                    make_mocked_coro((tr, proto))):
        req = ProxyClientRequest(
            'GET', URL('http://python.org'), loop=loop,
            proxy=URL('socks5://proxy.example'))
        connector = ProxyConnector(loop=loop, remote_resolve=False)
        connector._resolve_host = make_mocked_coro([mock.MagicMock()])
        conn = await connector.connect(req, [], ClientTimeout())

    assert connector._resolve_host.call_count == 2
    assert conn.protocol is proto

    conn.close() 
Example #8
Source File: test_dispatcher.py    From aiohttp_apiset with Apache License 2.0 6 votes vote down vote up
def test_url(dispatcher: TreeUrlDispatcher):
    location = dispatcher['root']
    assert 'path' in location.get_info()
    route = location._routes['GET']
    route.set_info(x=1)
    assert route.get_info().get('x') == 1

    location = dispatcher['pet']
    assert location.url(parts={'id': 1}) == '/api/1/pet/1'
    assert location.url_for(id=1) == URL('/api/1/pet/1')
    assert repr(location)

    route = location._routes['GET']
    assert route.url(parts={'id': 1}) == '/api/1/pet/1'
    assert route.url_for(id=1) == URL('/api/1/pet/1')
    assert repr(route)
    assert route.name
    assert 'formatter' in route.get_info()

    assert dispatcher.tree_resource.url_for() == URL('/')
    assert dispatcher.tree_resource.url(query={'q': 1}) == '/?q=1'
    assert repr(dispatcher.tree_resource)
    assert dispatcher.tree_resource.get_info() is not None 
Example #9
Source File: browser.py    From crocoite with MIT License 6 votes vote down vote up
def _responseReceived (self, **kwargs):
        self.logger.debug ('responseReceived',
                uuid='ecd67e69-401a-41cb-b4ec-eeb1f1ec6abb', args=kwargs)

        reqId = kwargs['requestId']
        item = self.requests.get (reqId)
        if item is None:
            return

        resp = kwargs['response']
        url = URL (resp['url'])
        logger = self.logger.bind (reqId=reqId, respUrl=url)
        if item.url != url:
            logger.error ('url mismatch', uuid='7385f45f-0b06-4cbc-81f9-67bcd72ee7d0', respUrl=url)
        if url.scheme in self.allowedSchemes:
            item.fromResponseReceived (kwargs)
        else:
            logger.warning ('scheme forbidden', uuid='2ea6e5d7-dd3b-4881-b9de-156c1751c666') 
Example #10
Source File: test_connector.py    From aiosocks with Apache License 2.0 6 votes vote down vote up
def test_proxy_connect_http(loop):
    tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
    loop_mock = mock.Mock()
    loop_mock.getaddrinfo = make_mocked_coro([
        [0, 0, 0, 0, ['127.0.0.1', 1080]]])
    loop_mock.create_connection = make_mocked_coro((tr, proto))
    loop_mock.create_task.return_value = asyncio.Task(
        make_mocked_coro([
            {'host': 'host', 'port': 80, 'family': 1,
             'hostname': 'hostname', 'flags': 11, 'proto': 'proto'}])())

    req = ProxyClientRequest(
        'GET', URL('http://python.org'), loop=loop,
        proxy=URL('http://127.0.0.1'))
    connector = ProxyConnector(loop=loop_mock)

    await connector.connect(req, [], ClientTimeout()) 
Example #11
Source File: browser.py    From crocoite with MIT License 6 votes vote down vote up
def fromRequestWillBeSent (self, req):
        """ Set request data from Chrome Network.requestWillBeSent event """
        r = req['request']

        self.id = req['requestId']
        self.url = URL (r['url'])
        self.resourceType = req.get ('type')
        self._time = ReferenceTimestamp (req['timestamp'], req['wallTime'])

        assert self.request is None, req
        self.request = Request ()
        self.request.initiator = req['initiator']
        self.request.headers = CIMultiDict (self._unfoldHeaders (r['headers']))
        self.request.hasPostData = r.get ('hasPostData', False)
        self.request.method = r['method']
        self.request.timestamp = self._time (req['timestamp'])
        if self.request.hasPostData:
            postData = r.get ('postData')
            if postData is not None:
                self.request.body = UnicodeBody (postData) 
Example #12
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_ipv4(self):
        u = URL("//127.0.0.1/")
        assert u.scheme == ""
        assert u.host == "127.0.0.1"
        assert u.path == "/"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #13
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_scheme_no_host(self):
        u = URL("scheme:/host/path")
        assert u.scheme == "scheme"
        assert u.host is None
        assert u.path == "/host/path"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #14
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_no_scheme_no_host(self):
        u = URL("scheme//host/path")
        assert u.scheme == ""
        assert u.host is None
        assert u.path == "scheme//host/path"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #15
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_weird_host(self):
        u = URL("//this+is$also&host!")
        assert u.scheme == ""
        assert u.host == "this+is$also&host!"
        assert u.path == "/"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #16
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_masked_ipv4(self):
        u = URL("//[127.0.0.1]/")
        assert u.scheme == ""
        assert u.host == "127.0.0.1"
        assert u.path == "/"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #17
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_complex_userinfo(self):
        u = URL("//user:pas:and:more@host")
        assert u.scheme == ""
        assert u.user == "user"
        assert u.password == "pas:and:more"
        assert u.host == "host"
        assert u.path == "/"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #18
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_weird_user2(self):
        u = URL("//user@info@ya.ru")
        assert u.scheme == ""
        assert u.user == "user@info"
        assert u.password is None
        assert u.host == "ya.ru"
        assert u.path == "/"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #19
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_weird_user(self):
        u = URL("//!($&')*+,;=@host")
        assert u.scheme == ""
        assert u.user == "!($&')*+,;="
        assert u.password is None
        assert u.host == "host"
        assert u.path == "/"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #20
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_strange_ip_3(self):
        u = URL("//v1.[::1]/")
        assert u.scheme == ""
        assert u.host == "::1"
        assert u.path == "/"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #21
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_absolute_no_scheme_simple_host(self):
        u = URL("//host")
        assert u.scheme == ""
        assert u.host == "host"
        assert u.path == "/"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #22
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_absolute_no_scheme_complex_host(self):
        u = URL("//host+path")
        assert u.scheme == ""
        assert u.host == "host+path"
        assert u.path == "/"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #23
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_absolute_no_scheme(self):
        u = URL("//host/path")
        assert u.scheme == ""
        assert u.host == "host"
        assert u.path == "/path"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #24
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_scheme_weird(self):
        u = URL("://and-this")
        assert u.scheme == ""
        assert u.host is None
        assert u.path == "://and-this"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #25
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_scheme_rel_path2(self):
        u = URL(":relative/path")
        assert u.scheme == ""
        assert u.host is None
        assert u.path == ":relative/path"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #26
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_scheme_rel_path1(self):
        u = URL(":relative-path")
        assert u.scheme == ""
        assert u.host is None
        assert u.path == ":relative-path"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #27
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_not_a_scheme2(self):
        u = URL("37signals:book")
        assert u.scheme == "37signals"
        assert u.host is None
        assert u.path == "book"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #28
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_not_a_scheme1(self):
        u = URL("not_cheme:path")
        assert u.scheme == ""
        assert u.host is None
        assert u.path == "not_cheme:path"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #29
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_no_scheme1(self):
        u = URL("google.com:80")
        assert u.scheme == ""
        assert u.host is None
        assert u.path == "google.com:80"
        assert u.query_string == ""
        assert u.fragment == "" 
Example #30
Source File: test_url_parsing.py    From yarl with Apache License 2.0 5 votes vote down vote up
def test_scheme_only(self):
        u = URL("simple:")
        assert u.scheme == "simple"
        assert u.host is None
        assert u.path == ""
        assert u.query_string == ""
        assert u.fragment == ""