🔍 Code Extractor

function _server_url

Maturity: 22

Constructs a properly formatted server URL by combining a base URL with a port number, handling both HTTP-prefixed and non-prefixed URLs.

File:
/tf/active/vicechatdev/patches/server.py
Lines:
66 - 70
Complexity:
simple

Purpose

This utility function normalizes URL construction for server endpoints by ensuring the URL has the correct protocol (http://), host, port, and trailing slash. It handles two cases: URLs that already start with 'http' (where it preserves the protocol and replaces the port) and URLs without a protocol (where it adds 'http://'). This is commonly used in web server initialization and configuration contexts, particularly for Bokeh server applications.

Source Code

def _server_url(url, port):
    if url.startswith("http"):
        return '%s:%d%s' % (url.rsplit(':', 1)[0], port, "/")
    else:
        return 'http://%s:%d%s' % (url.split(':')[0], port, "/")

Parameters

Name Type Default Kind
url - - positional_or_keyword
port - - positional_or_keyword

Parameter Details

url: The base URL string which can be in various formats: with or without 'http://' or 'https://' prefix, with or without an existing port number. Examples: 'localhost', 'http://example.com', 'https://example.com:8080', '192.168.1.1'

port: An integer representing the port number to use in the final URL. This will replace any existing port in the input URL or be added if no port was specified. Common values: 80, 443, 5006, 8080

Return Value

Returns a string containing the fully formatted server URL in the format 'http://host:port/' or 'https://host:port/' (if the original URL started with https). The URL always ends with a trailing slash. Examples: 'http://localhost:5006/', 'https://example.com:8080/'

Usage Example

# Example 1: URL without protocol
url1 = _server_url('localhost', 5006)
print(url1)  # Output: 'http://localhost:5006/'

# Example 2: URL with http protocol
url2 = _server_url('http://example.com', 8080)
print(url2)  # Output: 'http://example.com:8080/'

# Example 3: URL with https protocol and existing port
url3 = _server_url('https://example.com:443', 5006)
print(url3)  # Output: 'https://example.com:5006/'

# Example 4: IP address
url4 = _server_url('192.168.1.1', 3000)
print(url4)  # Output: 'http://192.168.1.1:3000/'

# Example 5: URL with existing port (no protocol)
url5 = _server_url('localhost:8000', 5006)
print(url5)  # Output: 'http://localhost:5006/'

Best Practices

  • This function assumes the port parameter is a valid integer; ensure port numbers are within valid range (1-65535)
  • The function preserves the protocol (http/https) if present in the original URL, but defaults to 'http' if no protocol is specified
  • Always returns a URL with a trailing slash, which is important for consistent URL handling in web applications
  • When the input URL contains a port, it will be replaced by the provided port parameter, not appended
  • The function uses rsplit with maxsplit=1 for URLs with 'http' to handle cases where the protocol itself contains a colon (http:// or https://)
  • For URLs without 'http', it uses split to remove any existing port before the new port is added

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function _origin_url 57.7% similar

    Removes the protocol scheme (http/https) from a URL string, returning only the domain and path portion.

    From: /tf/active/vicechatdev/patches/server.py
  • function get_server 48.0% similar

    Creates and configures a Bokeh Server instance to serve Panel applications with support for OAuth authentication, static file serving, and session management.

    From: /tf/active/vicechatdev/patches/server.py
  • function serve 44.2% similar

    Serves one or more Panel objects on a single web server, allowing interactive dashboards and applications to be deployed locally or remotely with configurable networking and threading options.

    From: /tf/active/vicechatdev/patches/server.py
  • function server_html_page_for_session 43.2% similar

    Generates a complete HTML page for a Bokeh server session by bundling resources and rendering document roots with session token.

    From: /tf/active/vicechatdev/patches/server.py
  • class SessionPrefixHandler 40.6% similar

    A mixin class that provides a context manager for temporarily modifying URL prefix-related state variables during request handling in a Bokeh server application.

    From: /tf/active/vicechatdev/patches/server.py
← Back to Browse