flask-proxy
h
JacksonCowie (149)

Creating A Simple Flask Proxy

It's quick, simple and relatively easy

I recently shared my flask proxy that I made on replit, it didn't get a lot of upvotes, so maybe a tutorial might get more upvotes?

First of all, I recommend that you read the 3 links below if you are not aware of the Flask Module, the requests module or what a Proxy is.

Read Those? Good lets begin.

Step 1-Imports

We need to import 2 Python Libraries, these are Flask and Requests, hopefully you have read about them and understand the basics of how they work and how they can be used.
So lets import them,

Easy...Right? Nothing too difficult here. Now lets continue.

Step 2-Variables

Ok, so now we need to create variables and assign some values to them, we will call them app and SITE_NAME.

In the "app" variable we need to create an instance of the Flask class for our proxy. __name__ is a special variable that gets as value the string __main__ when you’re executing the script. So we need to put in a value of Flask(__name__)

In the SITE_NAME variable we simply need to put in the full URL of the website that we wish to access. For example: https://discordapp.com/ So just put in whatever site you want.

Step 3-The Juicy Stuff

This is where stuff gets slightly more difficult, especially if you don't have knowledge in Flask or Requests.

So the first thing we need to do is define the route using the route Decorator of the Flask app Instance which we created in Step 2 On the same line we can add a rule to match the root directory which will also set the default value for the path argument. Here is what that line looks like.

Then we need to use the path converter to capture arbitrary length paths. <path:path> will capture the path and pass it to the path argument. The default converter captures a single string but stops at slashes. Which is why we need that line.

Next comes the part where we make use of the decorated(or view function). Luckily we have passed the most difficult part and we can go on to the easier part of the program...Wait! We haven't made use of the requests module yet, got you there didn't I?

First create the function with path as the parameter passed in then Simply add the line below that which is essentially just the website which is being accessed . We are then returning the result of that to the main program(Top Level, if you call it that) and that is the requests import done and dusted.

Step 4-Finally!

Finally, we can just add in a line all too familiar to us if __name__ == '__main__': , but if you don't know then, here's a short description.

Python assigns the name "main" to the script when the script is executed. If the script is imported from another script, the script keeps it given name (e.g. hello.py). In our case we are executing the script. Therefore, name will be equal to "main". That means the if conditional statement is satisfied and the (Spoiler Alert!)app.run() method will be executed. This technique allows the programmer to have control over script’s behavior.

If you read that previous paragraph then you will already know that all we need to do is run the app on the ip 0.0.0.0(The default route) on the TCP Port 8080.

Step 5-Conclusion

So, you have hopefully read it all now, hopefully you understand it and Hopefully I have made sense, this is actually really hard to write about and so this may not be the best tutorial, but better ones are to come.
The Github Repository for this tutorial can be found here https://github.com/Computer-Man497/Flask-Proxy
Thank You and Have A Great Day :)

You are viewing a single comment. View All
AdCharity (667)

@21natzil probably cause it is (I read it)