Jump to content

Python if possible? Redirect server request to URL list.

Lord Sexy
TLDR: I want a server to redirect you to a random url from a list.
 
Hey. Id like to make a service for my friends and me.
 
When accessed by invalid.com I want a RNG to select a url from possible_links.txt and send the visitor to that link.
every time my friends enter invalid.com they should end up at one of those videos.
Do I use a nginx webserver and send 303 redirects or how would one tackle this ?


I am not a noob, but close to. This project is my 4th on my rented server and if possible Id like to make it a short python script (at that point im not sure if that is possible) and not 69 dockercontainers to get a mysql database for realistically 100 links? maybe even less?

Much love and ill keep you informed.

Lord
Link to comment
Share on other sites

Link to post
Share on other sites

50 minutes ago, Sant_HH said:

Thank you sir. That is a very useful! Im sure Ill get everything I need from the manual for this one.

If I may show my lack of knowledge?

Is there a better way than just running it on port 5000 (?), using nginx to bend invalid.com to localhost:5000 and running the pythonscript with crontab @reboot? Or should I run crontab every 5 min, in case it freezes up?

Sorry. I never did something like that

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Lord Sexy said:

Is there a better way than just running it on port 5000 (?), using nginx to bend invalid.com to localhost:5000

Using a reverse proxy to forward incoming requests to a local service running on some other port is the common use case for it (the service itself generally shouldn't be reachable directly from the outside world).

 

A typical use case is to have nginx accept traffic for HTTP and HTTPS, then proxy it to one or more web services that only speak HTTP.

 

3 hours ago, Lord Sexy said:

running the pythonscript with crontab @reboot? Or should I run crontab every 5 min, in case it freezes up?

Running it on startup would be the preferred way. Anything else is a dirty hack. If your program freezes after 5 minutes, fix the program, rather than working around the issue.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Hey

Its me again.
I got it running on my PC

 

from flask import Flask, redirect, url_for
import random

app = Flask(__name__)

# List of URLs to choose from
url_list = [
    'https://youtube.com',
    'https://google.com',
    'https://9gag.com',
    # Add more URLs as needed
]

@app.route('/')
def redirect_to_random_url():
    # Select a random URL from the list
    random_url = random.choice(url_list)

    # Redirect the user to the random URL
    return redirect(random_url)

if __name__ == '__main__':
    # Run the Flask app on localhost:5000
    app.run(debug=True)



And if I use it on my windows machine and navigate to localhost:5000 it works fine.

But if I use it on a VPS or even my raspberry pi in my homenetwork it just doesnt work. I get some warnings about "This is not for productive environment", but ignoring that it sould be possible to find it in my local network.... There is no firewall or VLans. And if I can ssh into the pi i should be able to access port 5000, shouldnt I?

Best wishes,
Lord

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Lord Sexy said:

And if I can ssh into the pi i should be able to access port 5000, shouldnt I?

If you use an ssh tunnel (ssh <user@host> -L5000:localhost:5000), then yes you should. You could try something like "netstat -tulpen" to see if the program is listed as listening (is it listening on localhost or 0.0.0.0?). Maybe double check whether port 5000 isn't already in use by something else. A more common port for a program running behind a reverse proxy is 8080.

 

You may also want to read this: https://stackoverflow.com/a/54381386/7252334

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×