Categories > Coding > Python >

[REL] A simple web server hosted on your WiFi

Posts: 2014

Threads: 198

Joined: Apr, 2021

Reputation: 16

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. :)

  • 0

Random quote here...

nickk

RealNickk

Posts: 18

Threads: 1

Joined: Dec, 2022

Reputation: 7

Replied

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

  • 0

marcus__

S.K.I.D

vip

Posts: 444

Threads: 23

Joined: Nov, 2022

Reputation: 29

Replied

@nickk

using iSH right?

i use that a lot

  • 0

Added

@_realnickk

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

  • 0

!!!!!marcus__!!!!!#8611

https://cdn.discordapp.com/attachments/1066053366758780978/1078052772567597127/image.png

Alternate

stop take my rice

vip

Posts: 712

Threads: 113

Joined: Mar, 2022

Reputation: 40

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.

  • 0

we are dead

Posts: 2014

Threads: 198

Joined: Apr, 2021

Reputation: 16

Replied

@Alternate 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.

  • 0

Random quote here...

Alternate

stop take my rice

vip

Posts: 712

Threads: 113

Joined: Mar, 2022

Reputation: 40

Replied

@VoidableMethod

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.

  • 0

we are dead

Users viewing this thread:

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