[Python] Hosted script is being blocked by external webs on requests, but they can be done...

Discussão em 'Python' iniciado por Stack, Agosto 30, 2025.

  1. Stack

    Stack Membro Participativo

    I have a FastAPI server that connects and makes requests to the lyrics website genius.com via its library lyricsgenius. But since genius requires an API key for connecting with it, and I dont want my key to be stolen, I decided to host the script in a web service and then call it from there, so I could release only the frontend to the public (on a desktop app) without the key. The problem is that genius seems to reject my requests when they come from the hosted server, i.e. they return 403 with a huge html code that, according to Copilot, is some kind of captcha or anti-bot defense from Cloudflare. However, if I start the server locally and make requests from there, it works just fine. Am I trying to do something that is intentionaly forbidden, or there is a way I can achieve it? Here is a sample code of my server:

    import os
    import json
    import sys
    from fastapi import FastAPI
    from fastapi.responses import StreamingResponse
    from pydantic import BaseModel
    import lyricsgenius

    app = FastAPI()

    GENIUS_ACCESS_TOKEN = os.getenv("GENIUS_TOKEN")

    if GENIUS_ACCESS_TOKEN:
    # I tried adding an user_agent to modify the headers so Cloudflare doesnt think that I am a bot, but it seems not to work
    genius = lyricsgenius.Genius(
    GENIUS_ACCESS_TOKEN,
    remove_section_headers=True,
    skip_non_songs=False,
    user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
    )
    else:
    print("Fatal error: genius could not be started")
    sys.exit(1)


    async def buscar_cancion(cancion: Cancion):
    letra = ''
    error = ''

    try:
    # Here is where it fails
    song_genius = genius.search_song(cancion.titulo, cancion.artista)
    # ...
    except Exception as e:
    # ...
    print(e)

    return ...

    async def process_song_list(canciones: list[Cancion]):
    for cancion in canciones:
    resultado = await buscar_cancion(cancion)
    yield json.dumps(resultado) + '\n'


    @app.post("/api/procesar")
    async def procesar(canciones: Payload):
    return StreamingResponse(process_song_list(canciones.items), media_type="application/json")

    Continue reading...

Compartilhe esta Página