This content originally appeared on Level Up Coding - Medium and was authored by David Grigoryan
Not only web developers should know how to launch a local HTTP server but it 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 DTO models while the production backend part is still under development.
There are a lot of useful solutions for these tasks (like mockoon or postman) but in this article, we are going to build our own solution!
To launch the local web server we can use Debian or we can use macOS. In this tutorial, I am going to use Debian on Windows through WSL, but the vast majority of the presented commands are also applicable to the macOS ecosystem.
If you already have installed Debian OS we also need to install a python interpreter as well (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 also using Debian for this tutorial we might need to install a terminal multiplexer to make our further work more convenient:
$sudo apt install tmux
$brew install tmux (for the macOS users)
Once these operations are finished we can already launch our web server through the command listed below. For the Debian users we should start a new tmux session through 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. This will create a new terminal on the right side.
On macOS, you can create a new terminal window via CMD+N hotkey
You can also navigate through terminals with 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 some json file that will be sent to our mobile app whenever it’s requested. Let’s create this json file with nano:
$nano teachers.json
and paste this content:
For macOS users — you can just create a json file with any of the existing text editor in the newly created folder.
So now let’s launch our web server in this folder. At the left terminal run this command:
$python3 -m http.server
If the default port for this python web server (which is 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 ip address command for Debian or ifconfig for macOS. My server’s IP address is 192.168.1.53 so now I can switch to another device, open a browser and enter the following URL:
http://192.168.1.53:8000/teachers.json
Or we can test it through telnet on the right sight of our tmux session. Telnet also can be installed with this command:
$apt install telnet
$brew install telnet (for macOS)
As we can see it works correctly when we sending a common HTTP GET request and we are getting our response back!
The last thing left is to make sure we can get the response from our iOS app.
Let’s create a new Xcode project and in AppDelegate.swift file enter the following code:
Let’s start our 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 will be ready we can just replace our endpoints ?
Launching a handy python web server for mobile development was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by David Grigoryan
David Grigoryan | Sciencx (2021-05-06T14:43:59+00:00) Launching a handy python web server for mobile development. Retrieved from https://www.scien.cx/2021/05/06/launching-a-handy-python-web-server-for-mobile-development/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.