Beispiel #1
0
def test_parse_url():
    assert util.parse_url("example.com") == (None, "example.com", None, None)
    assert util.parse_url("https://example.com/test/test.php?x=1") == (
        "https", "example.com", None, "/test/test.php?x=1")
    assert util.parse_url("kdp://example.com:8080/test") == ("kdp",
                                                             "example.com",
                                                             8080, "/test")

    with pytest.raises(ValueError):
        util.parse_url("example.com:abcd")
Beispiel #2
0
async def connect(url, context=None, *, protocols=None):
    logger.debug("Connecting WS client to %s", url)

    scheme, host, port, path = util.parse_url(url)

    if scheme == "ws":
        context = None
    elif scheme == "wss":
        if context is None:
            context = tls.TLSContext()
    elif scheme is not None:
        raise ValueError("Invalid WS url scheme: %s" % scheme)

    if path is None:
        path = "/"

    server = util.make_url(None, host, port, None)
    async with http.connect(server, context) as client:
        async with util.create_task_group() as group:
            async with WebSocketClient(client, group) as client:
                await client.start_client(host, path, protocols)
                yield client
                await client.close()

    logger.debug("WS client is closed")
Beispiel #3
0
async def call(url, method, **kwargs):
	scheme, host, port, path = util.parse_url(url)
	if path is None:
		path = "/"
	
	req = HTTPRequest.build(method, path)
	
	for var in REQUEST_ARGS:
		if var in kwargs:
			setattr(req, var, kwargs.pop(var))
	
	if "Host" not in req.headers:
		req.headers["Host"] = util.make_url(None, host, port, None)
	
	url = util.make_url(scheme, host, port, None)
	return await request(url, req, **kwargs)
Beispiel #4
0
async def connect(url, context=None):
	scheme, host, port, path = util.parse_url(url)
	
	if scheme == "http":
		context = None
	elif scheme == "https":
		if context is None:
			context = tls.TLSContext()
	elif scheme is not None:
		raise ValueError("Invalid HTTP url scheme: %s" %scheme)
	
	if path is not None:
		raise ValueError("URL must not contain a path")
	
	if port is None:
		port = 443 if context else 80
	
	logger.debug("Establishing HTTP connection with %s:%i", host, port)
	async with tls.connect(host, port, context) as client:
		yield HTTPClient(client)