In the previous article, we've covered how to use Golang to create our webserver. In today's article, let's explain how to use Python to create a simple webserver on snappy Ubuntu. If you are not familiar with the snapcraft we use, please refer to the article "Use Snapcraft for our snappy Ubuntu application package". If you have a snap packet on ARM devices, please refer to the article "How to Compile and Package snap (2) for our snappy Ubuntu application."
1 Create our simple Python webserver
We can refer to some simpler Python webserver code. There's a lot of code on the Web. Today we are using one of these options:
server.py
#!/usr/bin/python from
basehttpserver import basehttprequesthandler,httpserver
port_number = 8080
#This Class would handles any incoming the request from
#the browser
class MyHandler (Basehttprequesthandler):
# Handler for the Get requests
def do_get (self):
self.send_response
self.send_header (' Content-type ', ' text/html ')
self.end_headers ()
# Send The HTML message
self.wfile.write ("Hello world!")
Return
try:
#Create a Web server and define the handler to manage the
#incoming request
Server = HTTPS Erver ((', port_number), MyHandler)
print ' started httpserver on PORT ', Port_number
#Wait forever for incoming Htto requests
Server.serve_forever ()
except keyboardinterrupt:
print ' ^c received, shutting down the Web server '
server.socket.close ()
We can enter the following command directly in our terminal:
$ python server.py
started httpserver on port 8080
If in our browser, we can access the address directly:
Obviously our webserver is running. In the next section, we'll pack the Python server into our snap.
2) Making Python webserver snap
In this section, we show how to make a Python webserver snap. First, we'll create our Snapcraft.yaml file in the same way that the article "uses Snapcraft to package our snappy Ubuntu apps". Our documentation is as follows:
Snapcraft.yaml
Name:pythonserver
version:1.0
Vendor:xiaoguo, Liu <xiaoguo.liu@canonical.com>
summary:a Simple Python server
description:this is the webserver API in Python demo
icon:icon.png
services:
mywebserver :
Description: "A webserver in Python"
start:bin/server
caps:
-network-client
- Network-service
Parts:
server:
plugin:python3
Source:.
Source:
plugin:copy
files:
./server.py:bin/server
Here, we use parts to install the Python runtime environment we need, and we also take our server.py to where we need it. Our Python webserver will run in a service way. Our entire project source code in the following address can be found:
Https://github.com/liu-xiao-guo/pythonserver
We have entered the following command in Termial to package:
$ snapcraft
So we can produce the snap package we need.
liuxg@liuxg:~/snappy/examples/pythonserver$ ls
icon.png parts pythonserver_1.0_amd64.snap server.py snap Snapcraft.yaml Stage
We can deploy it to our KVM environment:
(AMD64) ubuntu@localhost:~$ snappy list-v
Name Date Version Developer
ubuntu-core 2015-11-13 ubuntu*
ubuntu-core 2015-10-23 9 ubuntu
config-example 2015-11-30 Igbdlsndorod sideload*
Hello-xiaoguo 2015-11-05 ieenefbrvyge sideload
Hello-xiaoguo 2015-11-27 Igtctdaicyol sideload*
pastebinit 2015-11-02 1.4.0.0.2 mvo* pythonserver 2015-12-09 IHQDDRKVUAJC sideload
pythonserver 2015-12-09 ihqdjgyaasyy sideload*
restapi 2015-12-07 IHMFUOGUERWG sideload*
snappy-debug 2015-12-07 0.7 canonical* webdm 2015-10-23 0.9.2 canonical*
generic-amd64 2015-10-23 1.4 canonical*
(AMD64) ubuntu@localhost:~$ sudo Snappy Service Status
Snap service State
pythonserver mywebserver enabled; loaded; Active (running)
WEBDM snappyd enabled; loaded; active (running)
We can use our browser for testing. You can see a simple Python webserver like the one above.