Port Self-Reference for API Access #16096
-
I am working on an A1111 extension that uses the GET and POST CuRL commands of A1111's API. Specifically I am using the API to interact with another extension in the A1111 API. According to the API documentation I should use the default url Snippet of brute force identification of the hostname and port: def get_server_address(): # Brute force method
# Import the necessary modules
initialize_util = importlib.import_module('modules.initialize_util')
shared_cmd_options = importlib.import_module('modules.shared_cmd_options')
# Get server name
server_name = initialize_util.gradio_server_name()
if server_name is None: # Check for invalid value
server_name = "127.0.0.1" # Fallback to localhost
print(f"Server name determined: {server_name}")
# Determine port
port = shared_cmd_options.cmd_opts.port
print(f"Initial port: {port}")
if port is None:
ports = range(7860, 7870) # Check 7860 to 7869
for p in ports:
url = f"http://{server_name}:{p}/internal/ping"
try:
response = requests.get(url, timeout=5)
if response.status_code == 200:
port = p
print(f"Found active API on port: {port}")
break
except requests.RequestException as error:
print(f"API not available on port {p}: {error}")
if port is None:
port = 7860 # Default to 7860 if no active port is found
print(f"No active port found, defaulting to {port}")
# Return the server address
return f"http://{server_name}:{port}" I think I am severely over-complicating this and think there must be an easier way. I suspect I must have missed how I should be doing this when I was rooting around the modules. Can someone tell me how I should be doing this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
why don't you just set a static port?
ahh miss that part at the end of the day there is no way of knowing that if a server is running on a specific port of IP address add a extra complexity even by reading the command line you can't know the port if port is specified then it uses that port, but it will fail to Launch if the post is taken and if environment variable exists it could be possible that you can ask your OS which Port are in use and we used by which process on Windows you can use powersehll to all open ports on and what process is related to to be honest I think the best solution is to just ask the user to set a static Port because that's the best solution
|
Beta Was this translation helpful? Give feedback.
the way to get the IP/Port of the server
add a callback to on_app_started and read it from
demo.local_url
you can reference one of my extension https://github.com/w-e-w/sd-webui-custom-autolaunch/blob/ef4ab1b2e32c7ee8d8934b9ea2cbbf1a761fd686/scripts/custom_autolaunch_command.py#L70-L76