在前面的文章中,我们已经介绍了如何使用golang来创建我们的webserver.在今天的文章中,我们来介绍如何使用python在Snappy Ubuntu上创建一个简单的webserver.如果大家对我们使用的snapcraft还不是很熟的话,请参考文章"利用snapcraft为我们的Snappy Ubuntu应用打包".如果大家对如何在arm的装置上打snap包的话,请参考文章"如何为我们的Snappy Ubuntu应用编译并打包Snap(2)".
#!/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()
$ python server.py Started httpserver on port 8080
显然我们的webserver是正在运行的.在接下来的章节中,我们来把该python服务器来打包成为我们的snap.
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/server
https://github.com/liu-xiao-guo/pythonserver
$ snapcraft
liuxg@liuxg:~/snappy/examples/pythonserver$ ls icon.png parts pythonserver_1.0_amd64.snap server.py snap snapcraft.yaml stage
(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 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)