[GH-ISSUE #815] spotipy spins up a webserver that throws [Errno 48] Address already in use #507

Closed
opened 2026-02-27 23:23:00 +03:00 by kerem · 1 comment
Owner

Originally created by @ckahle33 on GitHub (May 31, 2022).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/815

I am using Spotipy in a Django application, and it was working fine for some time (likely the token age time) but now it is trying to spin up a development server on 120.0.0.1:8090 (as mentioned in app.py example), which is where Django is running. lsof indeed shows multiple listeners on that port:

➜  ~ lsof -i:8090
COMMAND     PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
Google    29820 anonnn   45u  IPv4 0x93a23e6b2bffd7d3      0t0  TCP localhost:52408->localhost:irdmi (ESTABLISHED)
Python    31091 anonnn    4u  IPv4 0x93a23e6b2e0b8d33      0t0  TCP localhost:irdmi (LISTEN)
Python    31091 anonnn    5u  IPv4 0x93a23e6b304d8293      0t0  TCP localhost:irdmi->localhost:52408 (ESTABLISHED)

This also happens when using port 8000 and 5000. How can I prevent the library from trying to spin up a server on the same port as Django?

Django Code / view functions

# spotify redirect url
def profile(request):
    # create django user here, and auth them
    cache_handler = spotipy.cache_handler.DjangoSessionCacheHandler(request)
    sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope='user-top-read',  cache_handler=cache_handler))

    playlists = sp.current_user_playlists()
    return render(request, 'accounts/profile.html', {'playlists': playlists['items']})

def login(request):
    return render(request, 'accounts/login.html')

def spotify_login(request):
    # sp = spotipy.Spotify(auth_manager=SpotifyOAuth())
    cache_handler = spotipy.cache_handler.DjangoSessionCacheHandler(request)
    auth_manager = SpotifyOAuth(scope='user-top-read',
                                                cache_handler=cache_handler, 
                                                show_dialog=True)
    return redirect(auth_manager.get_authorize_url())

Apologies if I am missing something. Thank you.

Originally created by @ckahle33 on GitHub (May 31, 2022). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/815 I am using Spotipy in a Django application, and it was working fine for some time (likely the token age time) but now it is trying to spin up a development server on 120.0.0.1:8090 (as mentioned in app.py example), which is where Django is running. lsof indeed shows multiple listeners on that port: ``` ➜ ~ lsof -i:8090 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME Google 29820 anonnn 45u IPv4 0x93a23e6b2bffd7d3 0t0 TCP localhost:52408->localhost:irdmi (ESTABLISHED) Python 31091 anonnn 4u IPv4 0x93a23e6b2e0b8d33 0t0 TCP localhost:irdmi (LISTEN) Python 31091 anonnn 5u IPv4 0x93a23e6b304d8293 0t0 TCP localhost:irdmi->localhost:52408 (ESTABLISHED) ``` This also happens when using port 8000 and 5000. How can I prevent the library from trying to spin up a server on the same port as Django? Django Code / view functions ``` # spotify redirect url def profile(request): # create django user here, and auth them cache_handler = spotipy.cache_handler.DjangoSessionCacheHandler(request) sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope='user-top-read', cache_handler=cache_handler)) playlists = sp.current_user_playlists() return render(request, 'accounts/profile.html', {'playlists': playlists['items']}) def login(request): return render(request, 'accounts/login.html') def spotify_login(request): # sp = spotipy.Spotify(auth_manager=SpotifyOAuth()) cache_handler = spotipy.cache_handler.DjangoSessionCacheHandler(request) auth_manager = SpotifyOAuth(scope='user-top-read', cache_handler=cache_handler, show_dialog=True) return redirect(auth_manager.get_authorize_url()) ``` Apologies if I am missing something. Thank you.
kerem 2026-02-27 23:23:00 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@ckahle33 commented on GitHub (Jun 1, 2022):

Looking at the stack trace, if get_access_token() is not manually called with the code from the callback query params, spotipy will attempt to spin up the server for you. Here is the complete working code in a Django context:

def login(request):
    return render(request, 'accounts/login.html')

# spotify redirect url
def callback(request):
    # todo: create django user here, and auth them
    cache_handler = spotipy.cache_handler.DjangoSessionCacheHandler(request)
    auth_manager = SpotifyOAuth(scope='user-top-read',
                                                cache_handler=cache_handler, 
                                                show_dialog=True)
    auth_manager.get_access_token(request.GET["code"])
    return redirect('/accounts/profile')


def profile(request):
    scope = 'user-top-read'   
    cache_handler = spotipy.cache_handler.DjangoSessionCacheHandler(request)
    sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, show_dialog=False, cache_handler=cache_handler))
    playlists = sp.current_user_playlists()
    
    return render(request, 'accounts/profile.html', {'playlists': playlists['items']} )

def spotify_login(request):
    cache_handler = spotipy.cache_handler.DjangoSessionCacheHandler(request)
    auth_manager = SpotifyOAuth(scope='user-top-read',
                                                cache_handler=cache_handler, 
                                                show_dialog=True)
    return redirect(auth_manager.get_authorize_url())
<!-- gh-comment-id:1143116545 --> @ckahle33 commented on GitHub (Jun 1, 2022): Looking at the stack trace, if `get_access_token()` is not manually called with the code from the callback query params, spotipy will attempt to spin up the server for you. Here is the complete working code in a Django context: ``` def login(request): return render(request, 'accounts/login.html') # spotify redirect url def callback(request): # todo: create django user here, and auth them cache_handler = spotipy.cache_handler.DjangoSessionCacheHandler(request) auth_manager = SpotifyOAuth(scope='user-top-read', cache_handler=cache_handler, show_dialog=True) auth_manager.get_access_token(request.GET["code"]) return redirect('/accounts/profile') def profile(request): scope = 'user-top-read' cache_handler = spotipy.cache_handler.DjangoSessionCacheHandler(request) sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, show_dialog=False, cache_handler=cache_handler)) playlists = sp.current_user_playlists() return render(request, 'accounts/profile.html', {'playlists': playlists['items']} ) def spotify_login(request): cache_handler = spotipy.cache_handler.DjangoSessionCacheHandler(request) auth_manager = SpotifyOAuth(scope='user-top-read', cache_handler=cache_handler, show_dialog=True) return redirect(auth_manager.get_authorize_url()) ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#507
No description provided.