一个简单的Snappy Ubuntu python webserver

来源:互联网 发布:mac版pscc2017已破解 编辑:程序博客网 时间:2024/04/28 21:13

在前面的文章中,我们已经介绍了如何使用golang来创建我们的webserver.在今天的文章中,我们来介绍如何使用python在Snappy Ubuntu上创建一个简单的webserver.如果大家对我们使用的snapcraft还不是很熟的话,请参考文章"利用snapcraft为我们的Snappy Ubuntu应用打包".如果大家对如何在arm的装置上打snap包的话,请参考文章"如何为我们的Snappy Ubuntu应用编译并打包Snap(2)".


1)创建我们的简单的python webserver



我们可以参考一些比较简单的python webserver代码.在网路上有很多的代码.今天我们就采用其中的一种方案:

server.py


#!/usr/bin/pythonfrom BaseHTTPServer import BaseHTTPRequestHandler,HTTPServerPORT_NUMBER = 8080#This class will handles any incoming request from#the browser class myHandler(BaseHTTPRequestHandler):#Handler for the GET requestsdef do_GET(self):self.send_response(200)self.send_header('Content-type','text/html')self.end_headers()# Send the html messageself.wfile.write("Hello World !")returntry:#Create a web server and define the handler to manage the#incoming requestserver = HTTPServer(('', PORT_NUMBER), myHandler)print 'Started httpserver on port ' , PORT_NUMBER#Wait forever for incoming htto requestsserver.serve_forever()except KeyboardInterrupt:print '^C received, shutting down the web server'server.socket.close()

我们可以直接在我们的terminal中打入如下的命令:

$ python server.py Started httpserver on port  8080

如果在我们的浏览器中,我们可以直接访问该地址:



显然我们的webserver是正在运行的.在接下来的章节中,我们来把该python服务器来打包成为我们的snap.


2)制作python webserver snap


在这一节中,我们来展示如何制作一个python webserver的snap.首先,我们来仿照在文章"利用snapcraft为我们的Snappy Ubuntu应用打包"介绍的那样来创建我们的snapcraft.yaml文件.我们的文件如下:

snapcraft.yaml

name: pythonserverversion: 1.0vendor: XiaoGuo, Liu <xiaoguo.liu@canonical.com>summary: a simple python serverdescription: This is the webserver API in python demoicon: icon.pngservices:  mywebserver:    description: "A webserver in python"    start: bin/server     caps:     - network-client     - network-serviceparts:  server:    plugin: python3    source: .  source:    plugin: copy    files:     ./server.py: bin/server

在这里,我们使用parts来安装我们所需要的python的运行环境,同时,我们也把我们的server.py考入到我们所需要的位置.我们的python webserver将会以一种service的方式来运行.我们的整个项目的源码在如下的地址可以找到:

https://github.com/liu-xiao-guo/pythonserver

我们在termial中打入如下的命令来进行打包:

$ snapcraft

这样我们就可以生产我们所需要的snap包.

liuxg@liuxg:~/snappy/examples/pythonserver$ ls icon.png  parts  pythonserver_1.0_amd64.snap  server.py  snap  snapcraft.yaml  stage

我们可以把它部署到我们的KVM环境中:

(amd64)ubuntu@localhost:~$ snappy list -vName           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 statusSnapServiceStatepythonservermywebserverenabled; loaded; active (running)webdmsnappydenabled; loaded; active (running)

我们可以使用我们的browser进行测试.可以看见和上面一样的一个简单的python webserver.



0 0
原创粉丝点击