Create a simple REST web service with Python--转载
今日尝试用python建立一个restful服务。
原文地址:http://www.dreamsyssoft.com/python-scripting-tutorial/create-simple-rest-web-service-with-python.php?/archives/6-Create-a-simple-REST-web-service-with-Python.html
Create a simple REST web service with Python
This is a quick tutorial on how to create a simple RESTful web service using python.
The rest service uses web.py to create a server and it will have two URLs, one for accessing all users and one for accessing individual users:
http://localhost:/users http://localhost:/users/{id}
First you will want to install web.py if you don't have it already.
sudo easy_install web.py
Here is the XML file we will serve up.
<users> <user id="1" name="Rocky" age="38"/> <user id="2" name="Steve" age="50"/> <user id="3" name="Melinda" age="38"/> </users>
The code for the rest server is very simple:
#!/usr/bin/env python import web import xml.etree.ElementTree as ET tree = ET.parse('user_data.xml') root = tree.getroot() urls = ( '/users', 'list_users', '/users/(.*)', 'get_user' ) app = web.application(urls, globals()) class list_users: def GET(self): output = 'users:['; for child in root: print 'child', child.tag, child.attrib output += str(child.attrib) + ',' output += ']'; return output class get_user: def GET(self, user): for child in root: if child.attrib['id'] == user: return str(child.attrib) if _name_ == "_main_": app.run()
To run your service, simply run:
./rest.py
This creates a web server on port 8080 to serve up the requests. This service returns JSON responses, you can use any of the following URLs to see an example:
http://localhost:/users http://localhost:/users/ http://localhost:/users/ http://localhost:/users/
Create a simple REST web service with Python--转载的更多相关文章
- Create screenshots of a web page using Python and QtWebKit | Roland's Blog
Create screenshots of a web page using Python and QtWebKit | Roland's Blog Create screenshots of a w ...
- 【转载】Using the Web Service Callbacks in the .NET Application
来源 This article describes a .NET Application model driven by the Web Services using the Virtual Web ...
- Customize the SharePoint 2013 search experience with a Content Enrichment web service
Did you ever wish you had more control over how your content is indexed and presented as search resu ...
- 【Java学习笔记】如何写一个简单的Web Service
本Guide利用Eclipse以及Ant建立一个简单的Web Service,以演示Web Service的基本开发过程: 1.系统条件: Eclipse Java EE IDE for Web De ...
- Part 17 Consuming ASP NET Web Service in AngularJS using $http
Here is what we want to do1. Create an ASP.NET Web service. This web service retrieves the data from ...
- Creating a Simple Web Service and Client with JAX-WS
Creating a Simple Web Service and Client with JAX-WS 发布服务 package cn.zno.service.impl; import javax. ...
- Python Web Service
搞移动端有段时间了,一直使用别人的API,自己也只写过ASP.NET网站作为网络服务,相对来讲是很大的短板.虽然ASP.NET可以提供想要的web服务,但是其体量臃肿,响应速度非常慢,这点我非常不喜欢 ...
- Python接口测试实战5(下) - RESTful、Web Service及Mock Server
如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...
- 使用 python 开发 Web Service
使用 python 开发 Web Service Python 是一种强大的面向对象脚本语言,用 python 开发应用程序往往十分快捷,非常适用于开发时间要求苛刻的原型产品.使用 python 开发 ...
随机推荐
- js判断访问者是否来自移动端代码
<script type="text/javascript"> function is_mobile() { var regex_match = /(nokia|iph ...
- gulp入门学习
一.gulp简介 gulp是一个自动化构建工具.在开发过工程中,能够使用gulp对项目进行自动构建,大大提高工作效率. 二.安装gulp 在安装gulp之前先要确认已经正确安装了node.js,然后在 ...
- Install the OpenStack command-line
Install the OpenStack command-line Install the prerequisite software python 2.7 or later note: Curre ...
- There is already an open DataReader associated with this Connection which must be closed first
使用MVC4 EF Linq获取foreach列表循环的时候遇到了如下的问题:报错提示 There is already an open DataReader associated with this ...
- MySQL分支Percona,折腾中,先科普一下
官方网站:http://www.percona.com/ Percona 为 MySQL 数据库服务器进行了改进,在功能和性能上较 MySQL 有着很显著的提升.该版本提升了在高负载情况下的 Inno ...
- 设计模式:模版模式(Template Pattern)
android中的Activity框架,View框架中大量的on函数基本上都应用到了Template模式,掌握这一模式对于理解这些框架大有裨益. 模版模式 又叫模板方法模式,在一个方法中定义一个算法的 ...
- github修改自己的昵称
由于刚接触github,不会用,就随便写了个昵称,后来想改,却不知道从哪里改,到百度搜结果都是说不能修改的(这里就不吐槽百度了),还是直接上图吧. 点击Settings,然后跳转到下面界面,点击Acc ...
- Linux系统文本命令快速登录与退出
Linux是一个多用户的操作系统,用户要使用该系统,首先必须登录系统,使用完系统后,必须退出系统.用户登录系统时,为了使系统能够识别自己,必须输入用户名和密码,经系统验证无误后方能进入系统.在系统安装 ...
- MD5加密运算
//MD5 对字符串的加密 -(void)demo1 { NSString *str = @"love"; //对字符串进行MD5加密 str = str.md5String; N ...
- VS2013打包与部署
近期做一个配置工具,完事了想打包一下:由于用的是VS2013:与之前的略有不同,简单的做了一下,在这里分享一下,直接看吧: 首先 是自己新建一个项目 ,我的WPF应用程序 第二步:右键解决方案添加新 ...