python web service

当遇到在不同平台,不同语言间进行开发时,难免需要用到web service。最近用到C和python两种语言,因此看了一下python开发web service。

用到的框架是flask,学习地址:http://docs.jinkan.org/docs/flask/

首先安装flask,直接pip install就可以了。

该方法中最简单的应用案例:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()
然后访问 http://127.0.0.1:5000/ (默认的),会看见 Hello World 问候。

稍微复杂一点的案例:

@app.route('/user/<username>')
def show_user_profile(username):
    # show the user profile for that user
    return 'User %s' % username

@app.route('/post/<int:post_id>')
def show_post(post_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id
可以在浏览器中输入username跳出自己输入的,输入ID也是一样的。

在C上的调用还在研究,等进一步更新。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值