Skip to content

Commit

Permalink
v2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed May 5, 2024
1 parent 04d6a95 commit b1cbf31
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions phi/cli/auth_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from phi.cli.settings import phi_cli_settings

import socket

class CliAuthRequestHandler(BaseHTTPRequestHandler):
"""Request Handler to accept the CLI auth token after the web based auth flow.
Expand Down Expand Up @@ -63,7 +62,7 @@ class CliAuthServer:
Source: https://stackoverflow.com/a/38196725/10953921
"""

def __init__(self, port=9191):
def __init__(self, port: int = 9191):
import threading

self._server = HTTPServer(("", port), CliAuthRequestHandler)
Expand All @@ -83,17 +82,25 @@ def shut_down(self):
self._thread.close() # type: ignore


def check_port(port):
def check_port(port: int):
import socket

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('localhost', port)) == 0

try:
return s.connect_ex(("localhost", port)) == 0
except Exception as e:
print(f"Error occurred: {e}")
return False


def get_port_for_auth_server():
if check_port(9191):
return 9191
starting_port = 9191
for port in range(starting_port, starting_port + 100):
if not check_port(port):
return port


def get_auth_token_from_web_flow(port) -> Optional[str]:
def get_auth_token_from_web_flow(port: int) -> Optional[str]:
"""
GET request: curl http:https://localhost:9191
POST request: curl -d "foo=bar&bin=baz" http:https://localhost:9191
Expand Down

0 comments on commit b1cbf31

Please sign in to comment.