Server Programming I

INFO 153B/253B: Backend Web Architecture

Kay Ashaolu

What is a Server?

  • A computer program or a device that provides functionality for other programs or devices, called "clients"
  • Servers can provide various functionalities, often called "services", such as sharing data or resources among multiple clients, or performing computation for a client
  • A single server can serve multiple clients, and a single client can use multiple servers

 

Server = "Computer Program"

  • A server is technically not the box that runs this computer program.
  • A server is a program that is hosted by a box (called a host)
  • We will learn how to write and run these programs to service "clients"
  • Example of clients include desktop browsers, mobile browsers, curl (via the terminal)

 

The Request-Reponse Model

  • Client sends a request to the Server, Server responds back with data and typically an acknowledgement of receipt
  • The Server has declared specific paths (called routes) that when accessed would execute specific functions
  • Functions would return a response, which is sent to the Client

 

Let's build our own server

  • We will be using Flask, a Python micro-framework that makes building web servers and API's easier

 

Flask: an application like everything else

  • Need to install it as a module of your Python installation
  • Once installed, you can use it in your Python program

 

Quick note on pip

  • Pip is a Python package manager that makes it easy to install new python modules
  • We use pip to install Flask and other Python packages (like Requests)

 

Let's install the python packages we need

pip3 install flask

First Python webserver app

# webserver.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
  return "Hello World!"

Let's create our first server

# Open a terminal and navigate to the folder where you have saved 
# your server code

python3 -m flask --app webserver run --host=0.0.0.0 --port=5050

What did that do?

  • You just created your own web server!
  • Go to localhost:5050 on your browser to see it work

 

 

What did that do?

  • python -m: gives you the ability to run a specific module (e.g. file) that has been already installed (via pip3 or in the PYTHONPATH)

  • --app [app name] Flask parameter specifying which python module the Flask app to run
  • --host=0.0.0.0 -> this enables the server to be visible externally (e.g. outside of your computer)
  • --port=5050 -> changes the port number. I find these days the default (5000) seems to be used for something else that's running on your computer

 

 

What's going on?

  • We first imported the Flask class from the flask module that we just installed
  • Next we created a variable that points to a new flask application
  • We then set up a route (the root "/" route)
  • We defined a function that if someone goes to the root of our webserver, then the server should send the string "Hello World"

Note about routes

  • Routes associate a path to a function that sends a response back to the client
  • So if a client asks for the root path: http://localhost:5050/, it will run the above function
  • The response body will be "Hello World"
@app.route('/')
def hello_world():
  return "Hello World!"

Questions?