paint-brush
Launching a Python Webserver for Mobile Development by@davidgg
505 reads
505 reads

Launching a Python Webserver for Mobile Development

by David GrigoryanMay 25th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

To launch the local webserver, we can use Debian or macOS to launch the webserver. For example, we might want to check the network layer of our mobile app and parse the response to our DTO models while the backend part is still under development. There are many useful solutions for these tasks (such as mockoon or postman) However, in this article, we are going to build our own solution to these tasks. In this tutorial, I will use Debian on Windows through WSL, though the vast majority of the commands are also applicable to the macOS ecosystem.

Company Mentioned

Mention Thumbnail
featured image - Launching a Python Webserver for Mobile Development
David Grigoryan HackerNoon profile picture
Not only should web developers know how to launch a local HTTP server, but it's also useful for mobile developers as well. For example, we might want to check the network layer of our mobile app and parse the response to our  models while the backend part is still under development.There are many useful solutions for these tasks (such as  or ). However, in this article, we are going to build our own solution! 🤓💪  

To launch the local webserver, we can use Debian or macOS. In this tutorial, I will use Debian on Windows through WSL, though the vast majority of the listed commands are also applicable to the macOS ecosystem.

If you have already installed Debian OS, we also need to install a python interpreter (This procedure is not required for macOS users because macOS already has a python interpreter). We can do this via this command:
$sudo apt install python3
If you are using Debian for this tutorial, you might need to install a terminal multiplexer to make further work more convenient:
$sudo apt install tmux
$brew install tmux (for the macOS users)

Once these operations are finished, you can launch the webserver through the command listed below. For the Debian users, we should start a new

tmux
session with this command:

$tmux new-session -s testname

And now, we can create a separate terminal to debug our server. We can do this using CTRL+B hotkey and right after that - press % (I know that's a weird hotkey 🤯).

This will create a new terminal on the right side. In macOS, you can create a new terminal window via CMD+N hotkey.

You can also navigate through

tmux
sessions using CTRL+left key/right key hotkey.

Then create a folder named “server” and change the directory to this newly created folder:
$mkdir server
$cd server

Right after that, we need to create a json file that will be sent to our mobile app whenever requested. Let’s create this json file with 

nano/vim
:

$nano teachers.json
and paste this content:
{  "success": true,  
   "body": [
    {      
      "id": "1",
      "first_name": "Julia",
      "last_name": "Green"    
    },    
    {
      "id": "2",
      "first_name": "Peter",
      "last_name": "Sheppard"
    } 
]}
For macOS users: You can just create a json file with any existing text editor in the newly created folder.So now, let’s launch the webserver in this folder. At the left terminal, run this command:
$python3 -m http.server
If the default port for this python webserver (8000) is not occupied by another process, then the launching process should be finished successfully. Otherwise, you can provide a different port like this:
$python3 -m http.server 8052

The last thing we need to do on our “backend” side is to verify the reachability of our service. First of all, we need to get our server’s local IP address. You can find it in the

ip address
 command for Debian or 
ifconfig
 for macOS. My server’s local IP address is 192.168.1.53, so now I can switch to another device, open a browser and enter the following URL:

//192.168.1.53:8000/teachers.json

Or you can test it through 

telnet
 on the right side of the
tmux
session. Telnet also can be installed with this command:

$apt install telnet
$brew install telnet (for macOS)

As you can see here, I am sending a common HTTP

GET
request on the left session, and as a result, you can see the response on the right side!

The last thing is to make sure you can get the response from our iOS app.
So, let’s create a new Xcode project, and in AppDelegate.swift file, enter the following code:

  func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        var components = URLComponents()
        components.scheme = "http"
        components.host = "192.168.1.53"
        components.port = 8000
        components.path = "/teachers.json"
        // Which finally brings us //192.168.1.53:8000/teachers.json
        if let url = components.url {
            URLSession.shared.dataTask(with: url) { data, response, error in
                if let error = error {
                    print(error)
                } else if let data = data,
                          let jsonResponse = try? JSONSerialization.jsonObject(with: data,
                                                                               options: .allowFragments) {
                    print(jsonResponse)
                }
            }.resume()
        }
        return true
    }
Let’s start the app and see the result…

And that’s it! We finally got the response from our local server! 🎉🎉🎉
Now we can continue our development process with these mocks, and when the real server side is ready, we can just replace our endpoints!🙂

바카라사이트 바카라사이트 온라인바카라