Categories > Coding > Python >

[REL] A simple web server hosted on your WiFi


New Reply

Aja

aka VoidableMethod

Posts: 1667

Threads: 154

Joined: Apr, 2021

Reputation: 5

  • 0

Posted

Hi!
So I recently came up with the idea of hosting a little Arch server on my old laptop. And I did it, but I don't have any ideas what can I use it for. So I used it for a little web server. It's very very easy and simple to make, all you need is Python and flask installed.

 

Source code:

import flask

from flask import Flask

app = Flask(__name__)

@app.route("/")
def main():
    return "Hello, world!", 200

 

To run the server on your local network, run this command:

 

flask run --host=0.0.0.0

 

To make sure this command will work, make sure your web server file is called "app.py", otherwise flask will not find the file.

 

Now, to access the website, open cmd, and type the command ipconfig. Find the "IPv4 Address" line, and in the browser paste the address that is in there. It should start with 192.168.

 

If you're on Linux (ex. Arch), run ip a. Find the WiFi adapter you use, and as before, find the address that should start with 192.168. Then, access that URL.

 

If you have any questions (or suggestions!), feel free to ask. :)

nickk

RealNickk

Mention

Posts: 30

Threads: 1

Joined: Dec, 2022

Reputation: 7

  • 0

Replied

I ran this on my iPhone for no reason at all lol.

marcus__

#MakeBjornAdmin

patron Mention

Posts: 290

Threads: 13

Joined: Nov, 2022

Reputation: 21

  • 0

Replied

@nickk

using iSH right?

i use that a lot

bennytrt, ishanjit, immunelion, Lxnnyy & aeon r cool

Ws: Atonix, Syron, Oxygen U, Nihon.

#MakeBjornAdmin

Posts: 1234

Threads: 64

Joined: Feb, 2020

Reputation: 69

  • 0

Replied

Correct. I use it quite a bit when I want to tether celluar to wifi without my carrier charging me 50 extra dollars a month for using hotspot.

marcus__

#MakeBjornAdmin

patron Mention

Posts: 290

Threads: 13

Joined: Nov, 2022

Reputation: 21

  • 0

Replied

@RealNickk

a lot of people use iSH
i wish someone made something where I could run something like ubuntu touch

bennytrt, ishanjit, immunelion, Lxnnyy & aeon r cool

Ws: Atonix, Syron, Oxygen U, Nihon.

#MakeBjornAdmin

Posts: 481

Threads: 74

Joined: Mar, 2022

Reputation: 12

  • 0

Replied

This isn't hosted on your WiFi lmao, as someone who uses Flask a lot it's always hosted on a physical machine, and WiFi isn't a physical machine. You can also host flask on a custom domain (ex. flaskapp.com) but that's more advanced. Also, you don't have to run the command

flask run --host=0.0.0.0

Instead, you can actually put this at the bottom of your code:

app.run(host='0.0.0.0', port=[PORT])

And then just run

python app.py

OR

python3 app.py

By adding that to the bottom of your code you can also rename the Python file and by just running the file directly it will still work.

  • 0

Added

Also, you don't need to put ", 200" on the return statement. Simply doing

return "Hello, world!"

will produce the same result and adding ", 200" to it is just adding unneccessary code.

 

Flask will also tell you the localhost address it's running off, so if you're testing or only want to access it yourself follow the localhost address it provides when you run it.

  • 0

Added

Also I wouldn't release something like this, it's extremely basic and even the most incompetent skids can produce this work (without skidding). If you make something good with Flask, then release it, all this does it show Hello World.

test_bot2 on top

Aja

aka VoidableMethod

Mention

Posts: 1667

Threads: 154

Joined: Apr, 2021

Reputation: 5

  • 0

Replied

@AlternateD It is hosted on the local WiFi, accessible by devices on your local network. I know this is very simple, so were your projects. I am trying to simply contribute to the forum, that's what I am doing. Both the commands and the code works, I used the command because it's simply easier to use in my opinion.

Posts: 481

Threads: 74

Joined: Mar, 2022

Reputation: 12

  • 0

Replied

@Aja

The code works but some of it just doesn't make sense to be using. Simply running the python file is usually easier than a longer command imo, it doesn't make sense to me as to why you would do that.

 

I'm not trying to argue but, for a Flask developer, this is basically a slightly overcomplicated Hello World script. The most basic project I've released was a password generator- and I believe I was only the second person to do so. Even such code serves a better purpose than this, no offense. If you want to contribute to the forum, create something with this and then release it. Releasing what is again essentially a slightly overcomplicated Hello World script isn't very beneficial to the forum.

 

Also, the Flask server and code itself is hosted on your local machine, not the WiFi. That is what I am referring to.

test_bot2 on top


New Reply

Users viewing this thread:


( Members: 0, Guests: 1, Total: 1 )