今日尝试用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--转载的更多相关文章

  1. 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 ...

  2. 【转载】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 ...

  3. 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 ...

  4. 【Java学习笔记】如何写一个简单的Web Service

    本Guide利用Eclipse以及Ant建立一个简单的Web Service,以演示Web Service的基本开发过程: 1.系统条件: Eclipse Java EE IDE for Web De ...

  5. 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 ...

  6. 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. ...

  7. Python Web Service

    搞移动端有段时间了,一直使用别人的API,自己也只写过ASP.NET网站作为网络服务,相对来讲是很大的短板.虽然ASP.NET可以提供想要的web服务,但是其体量臃肿,响应速度非常慢,这点我非常不喜欢 ...

  8. Python接口测试实战5(下) - RESTful、Web Service及Mock Server

    如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...

  9. 使用 python 开发 Web Service

    使用 python 开发 Web Service Python 是一种强大的面向对象脚本语言,用 python 开发应用程序往往十分快捷,非常适用于开发时间要求苛刻的原型产品.使用 python 开发 ...

随机推荐

  1. Dijkstra算法and Floyd算法 HDU 1874 畅通工程续

    Dijkstra算法描述起来比较容易:它是求单源最短路径的,也就是求某一个点到其他各个点的最短路径,大体思想和prim算法差不多,有个数组dis,用来保存源点到其它各个点的距离,刚开始很好办,只需要把 ...

  2. ASP.NET-FineUI开发实践-6

    FineUI4.1.0更新,传说的V4版稳定版,很多人也从3.0+升级了,接着又连续更新了几次,现在是V4.1.3 2014-09-09日更新的.更新的挺快,感觉跟不上节奏,我很欣慰,看来开原版还是靠 ...

  3. NYOJ737

    题意:给n堆石子,按照顺序排列,只能相邻两堆石子合并,求最后合并为一堆时所花费的最小代价,石子合并代价为两堆石子之和. 输入: n(石子堆数) Xi(每堆石子个数) 输出: T(最小代价) 思路:经典 ...

  4. 一些YY

    都说苦痛难熬,哪里想过苦痛也是良药. 现在想起什么就说什么吧,反正自己还很弱,没有身高,没有长相,家里不富,学习也是渣,,, 大一的时候自己也很努力的去学习,去实践,但是成绩不理想,我就在想也许时间还 ...

  5. vs连接mysql

    1.打开vs2012在aspx中添加一个Grid view 控件,,. 2,选择新建数据源. 3,选择数据库. 4,选择新建连接. 5,更改成mysql连接. 6,这里的Server name 是你自 ...

  6. GridView and DropDownList

    <form id="form1" runat="server"> <div> <asp:GridView runat=" ...

  7. CSS3 3D转换

    CSS3允许你使用3D转换来对元素进行格式化. 3D转换方法: rotateX() rotateY() 浏览器支持 属性 浏览器支持 transform           IE10和Firefox支 ...

  8. Arcgis Desktop 9.3 安装

    以下用到的 Crack在我的网盘中有: ref: http://pan.baidu.com/s/1pJJlVBl 密码: p4gk 一,安装 Desktop(依次按照如图安装): 二,配置 1,以上步 ...

  9. 安装VS2010后,如何设置老版本的项目文件不是默认用VS2010打开

    1.系统先后安装了VS2008和VS2010,在打开用VS2008创建的项目文件时总是会默认用VS2010打开,选择打开方式都不行,很不方便,差点要把VS2010卸载了.     其实只需要简单设置V ...

  10. mysql 数据库字符串替换

    UPDATE `table_name` SET `field_name` = replace (`field_name`,'from_str','to_str') WHERE `field_name` ...