iterative / iterative/PyDrive2

LocalWebserver stuck if some other application is running on windows

Open
#378 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
670
Forks
75
PR merge metrics
No merged PRs in 30d

Description

Hi,

When something is already running on port 8080, calling:

`gauth.LocalWebserverAuth()
`
successfully opens the browser for OAuth, but after granting access, the redirect to http://localhost:8080/?code=... fails with ERR_CONNECTION_REFUSED.
The command line then just waits indefinitely for the code, because the local server never started on that port.

This happens because LocalWebserverAuth tries to bind to 8080 without checking if it’s free.

Proposed fix:
Check if the port is available before binding, and try alternatives in a small range (e.g., 8080–8085) before raising an error. Example using portpicker:
```python

import portpicker

candidate_ports = [8080] + list(range(8081, 8086))
port_number = None
for p in candidate_ports:
if portpicker.is_port_free(p):
port_number = p
break

if port_number is None:
raise RuntimeError("No free port in range 8080–8085")

print(f"Using port {port_number}")
```

If a non-default port is selected, it would also need to be reflected in the OAuth URL so the redirect URI matches.
Users can pre-register multiple redirect URIs in Google Cloud Console to cover the fallback ports.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.