In the previous article, we showed you how to use Golang to create our WebServer. In today’s article, we’ll show you 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 “Using Snapcraft to package our Snappy Ubuntu application”. For more information on how to package Snap 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 of the simpler Python WebServer code. There is a lot of code on the Internet. Today we are going to use one of them:

\

server.py

\

#! /usr/bin/python from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer PORT_NUMBER = 8080 #This class will handles  any incoming request from #the browser class myHandler(BaseHTTPRequestHandler): #Handler for the GET requests def do_GET(self): self.send_response(200) 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 = HTTPServer(('', 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()Copy the code

We can directly type the following command in our terminal:

\

$ python server.py 
Started httpserver on port  8080
Copy the code

If in our browser, we can access the address directly:

\

\

\

Obviously our WebServer is up and running. In the following sections, we’ll package this Python server as our Snap.

\

2) Make python WebServer Snap

\

In this section, we’ll show you how to make a Python WebServer snap. First, let’s create our snapcraft. Yaml file as described in the article “Using Snapcraft to package our Snappy Ubuntu application”. Our documents are as follows:

\

snapcraft.yaml

Name: pythonServer version: 1.0 Vendor: XiaoGuo, Liu <[email protected]> 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/serverCopy the code

Here, we use parts to install the Python runtime we want, and we also check our server.py into the location we want. Our Python WebServer will run as a service. The source code for our entire project can be found at the following address:

\

https://github.com/liu-xiao-guo/pythonserver
Copy the code

We type the following command in Termial to pack:

\

$ snapcraft
Copy the code

So we can produce the Snap packages we need.

\

Liuxg @ liuxg: ~ / after/examples/pythonserver $ls icon. The PNG with parts pythonserver_1. 0 _amd64. The snap for server py snap snapcraft. Yaml stageCopy the code

\

We can deploy it in our KVM environment:

\

(amd64)ubuntu@localhost:~$ snappy list -v Name Date Version Developer ubuntu-core 2015-11-13 10 ubuntu* ubuntu-core 2015-10-23 9 ubuntu config-example 2015-11-30 IGbdLSNDOROd sideload* hello-xiaoguo 2015-11-05 IEeNEfBRVYGe sideload Pastebinit * 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)Copy the code

\

We can test this using our Browser. You can see a simple Python webServer.\ as above

\

\