Build A Python Server Image

Warning

Some activities involve using command prompt.

Are you ready to build a python server? Let’s get started!

1, Part 1. Things To Install:

Time to check if you have Python. Windows 10 Users: Type Python into the search bar at the bottom of the screen. If you see a Python application, it is already installed; You can go to the next section. If you do not see a Python application, or it searches the web, go to python.org. Then, rollover Downloads and click on the Download for Windows button. During installation, click on Advanced Options. Make sure the box that is about adding Python to your PATH is checked.

Python on your PATH. Windows 10 Users: Type CMD into the search bar. In CMD, type:

python

If you see python start up, Python is on your PATH. If it doesn’t, sorry, you can not do this activity yet. I will add info about adding it to your path later.

1, Part 2. Things To Install:

Time to install Flask! Windows 10 Users: Type CMD into the search bar at the bottom of the screen. (You might need to run it as an administrator.) In CMD type:

pip install Flask

If it says permission denied, go back and run CMD as an administrator. If it says Flask is already installed, type:

pip install update Flask

Flask is now updated and installed. Close the CMD window.

2. Make the Code:

Create a folder. Name it server. Inside the file space you are using, create the folders “style” and “page”. Open Python Idle. Create a new program called “app” in the server folder. Type:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def main():
    return render_template('main.html')

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

Now, save the code and open another Python Idle window. Create a file, select All Files instead of Python File, and name it “main.html”. (Do not forget the html extension.) Type:

<html>
<body>
<h1>Hello!!!</h1>
<p>This is my website!</p>
</body>
</html>

3. Run the Server:

Now, time to try the site! Open CMD and type

ipconfig

Find the type of WiFi connection you use. Copy the IPv4 address. Open a web browser tab. Paste you ip address with :5000 on the end. Do not go to the site yet. Type:

cd server
python app.py

It should be running if the command prompt windows shows information without any errors. Now, go to the site. It should be running! (Note: the security of the server has not been tested.) Press CTRL+C to stop the server. Note: The server can only be accessed anyone on your local network.

4. Color:

Does your site seem kind of bland? You can add some color. Remember that style folder? Go to Python Idle. Create a new file, change it to All Files instead of Python Files, and name it “mainstyle.css”. Type:

body {
    background: blue;
    color: green;
}

Edit main.html in IDLE. Change the code to:

<html>
<head> 
<link rel="stylesheet" href='/static/mainstyle.css' /> 
</head>
<body>
<h1>Hello!!!</h1>
<p>This is my website!</p>
</body>
</html>

Now, the background is blue and the text is green.

5, Part 1. A Quick Trick:

Here is a trick for a new page. Open App.py in IDLE and change the code to:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def main():
    return render_template('main.html')

@app.route('/welcome/<name>')
def hello(name):
    return render_template('welcomeperson.html', name=name)

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

5, Part 2. A Quick Trick:

In the page folder create another html file named “welcomeperson.html”. Type:

<html>
<head>
<link rel="stylesheet" href='/static/mainstyle.css' />
</head>
<body>
<h1>Welcome {{ name }}!</h1>
<p>This is my website.</p>
</body>
</html>

Run your site. go to “yourip:5000/welcome/name”, replacing “yourip” with your ip address and “name” with a name. It should say welcome and the name you picked. Now you have finished the “Build A Python Server” tutorial!

Hope you enjoyed the tutorial!

(Related Pages: Coding Books and Windows 11 Virtual Machine)