今日尝试用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. Java基础知识强化97:final、finally、finally区别

    1. final修饰符(关键字)     如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此,一个类不能既被声明为abstract,又被声明为final.     将 ...

  2. EF 已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭

    在以下代码中,当第二次foreach时会抛出该异常,原因是:由于Entity在读取数据的时候使用的是DbDataReader进行读取,当作为IEnumuerable<T>对象MoveNex ...

  3. RadioGroup单选按钮排版

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  4. ViewState探索

    什么是 view state? View State是客户端状态管理重要机制之一.当页面PostBack(向服务器发送或获得数据)时,它能存储页面的值.ASP.NET把View State属性作为页面 ...

  5. Nginx配置文件nginx.conf详解(转)

    #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | ...

  6. 可清空文本的EditText

    代码如下: public class DeleteEditText extends EditText { private Context mContext; //删除图标 private Drawab ...

  7. Ext江湖笔记:JavaScript基本知识点

    1.基本对象:Number,String,Date,Array,Error,RegExp,Math,Boolean ps:本人基本使用java写代码,常常写出Number n = new Number ...

  8. Lua 基础知识-面向对象

    通过函数闭包的方式来实现面向对象 -- 通过函数闭包的方式来实现面向对象 function People(name) local self = {} local function init() sel ...

  9. angularjs中的绑定策略“@”,“=”,“&”实例

    <!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...

  10. 【C#编程基础学习笔记】6---变量的命名

    2013/7/24 技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com [C#编程基础学习笔记]6---变量的命名 ----- ...