72

hello大皮——微型 python web 框架:Bottle (一)

 4 years ago
source link: https://www.tuicool.com/articles/322AZvZ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Bottle  是一个非常小巧但高效的微型 Python Web 框架,它被设计为仅仅只有一个文件的 Python 模块,并且除 Python 标准库外,它不依赖于任何第三方模块 ( 摘抄完毕 )~

我的应用场景:

局域网内,需要从一台机器(ubuntu系统 ) 传输数据到另一台上。

解决方案可以有很多,但python的 bottle 绝对是小巧高效的选择。

系统平台:ubuntu、 python2.7    需安装 python bottle 模块

上码:【代码过于简单,可能引起不适】

from bottle import route, request, run


@route('/hello', method=['GET', 'POST'])

def dh():

return 'hello ' + request.query.str

if __name__ == "__main__":

run(host='0.0.0.0', port=8080)

导入bottle三个组件: route, request, run

/hello   这个 URL 地址绑定到 dh() 函数,任何对“ /hello ”这个 URL 的请求都将被递交到 dh() 这个函数中

获得请求后返回字符串,这里的 request.query.str 是传入参数

最后,run() 函数启动服务器,并且我们设置它在 本机 的 8080 端口上运行

运行结果:

①启动 python 服务效果

AbYJzmJ.jpg!web

②网页中输入 URL及运行效果

JzAnaaf.png!web

?str=大皮 传入参数的写法

后记:也可以使用Qt 的 QNetworkAccessManagerQNetworkReply 来请求 URL 的结果,下次和大家一起分享 Qt 的方法;有些智能相机传输图片用的就是这种方式~~

欢迎大家交流分享~

eIZJ3iQ.jpg!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK