今日尝试用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. Ubuntu 14.04 配置vsftpd实现FTP服务器 - 通过FTP连接AWS

    测试主机:亚马逊AWS EC2 系统:Ubuntu 14.04 想用AWS来做服务器玩,结果发现其不能像简单使用阿里云服务器那样用ftp连接,反正也不熟悉ftp服务器搭建,那就乘这个机会学习一下如何利 ...

  2. Sql 中常用日期转换Convert(Datetime)

    CONVERT(data_type,expression[,style]) convert(varchar(10),字段名,转换格式) 说明:此样式一般在时间类型(datetime,smalldate ...

  3. json数据返回

    <script type="text/javascript"> function xmlpage(){ var xhr=new XMLHttpRequest(); xh ...

  4. java Byte.toString 方法与String.ValueOf(Byte)效率比较

    int times = 10000000; Byte[] li = new Byte[times]; for (int i = 0; i < times; i++) { li[i] = (byt ...

  5. Knockoutjs官网翻译系列(二) Observable 数组

    承接前文,前文书说道了KO框架中如何使用observable的视图模型属性来与UI元素进行绑定并自动进行双向更新的事儿.observable属性除了服务基础数据类型之外,还定义了专门为服务数组类型的o ...

  6. Java中的内部类、匿名类的使用

    代码(test.java): interface ie{ public void print(); } class outer{} public class test{ public class in ...

  7. Flume 1.4.0 User Guide

    Apache Flume is a distributed, reliable, and available system for efficiently collecting, aggregatin ...

  8. cocos2d-x创建新项目模板

    1.起因 长期使用项目中自带的HelloWorldScene来创建模板工程,不知大家有木有感到厌烦? 我是个懒人,所以就弄了个新的模板工程.这样最起码可以不用每次都把HelloWorldScene删掉 ...

  9. java动态缓存技术:WEB缓存应用(转)

    可以实现不等待,线程自动更新缓存 Java动态缓存jar包请下载. 源代码: CacheData.java 存放缓存数据的Bean /** *  */package com.cari.web.cach ...

  10. 移动端(html5)微信公众号下用keyup实时监控input值的变化无效

    搜索框依据用户输入的值实时检索,一开始自然而然想到keyup,在拼音状态时,啥问题也没有, 问题1:切换到中文输入法,问题出来了,keyup事件不灵便了,后来在网上搜了下,找到了思路, 问题2:微信公 ...