今日尝试用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. Configuring the JA-SIG CAS Client --官方

    1. for Java using Spring Configuration of the CAS Client for Java via Spring IoC will depend heavily ...

  2. linux系统中中断已连接的用户

    1.用w命令查看当前系统登录的用户 [root@rhel7 ~]# w :: up :, users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOG ...

  3. VB中的Dictionary对象

    VB中的Dictionary对象 Dictionary对象不是VBA或Visual Basic实时语言的具体存在的部分,它是存在于Microsoft Scripting Runtime Library ...

  4. 简要介绍 My.Ioc 的用法

    下面这段代码展示了 My.Ioc 的基本用法: using System; using System.Collections.Generic; namespace My.Ioc.Sample { pu ...

  5. ORACLE输出详细错误信息错误行数

    ... COMMIT; --输出成功信息 DBMS_OUTPUT.PUT_LINE('RUN RESULT: SUCCESS'); EXCEPTION WHEN OTHERS THEN BEGIN R ...

  6. Android ScrollView 嵌套 ListView、 ListView 嵌套ScrollView Scroll事件冲突解决办法

    本人菜鸟一名,最近工作了,开始学习Android. 最近在做项目的时候,UX给了个design,大概就是下拉刷新的ListView中嵌套了ScrollView,而且还要在ScrollView中添加动画 ...

  7. mvc wcf 并发提示,存储Application,验证是否有用户在操作

    System.Web.HttpContext httpContext = System.Web.HttpContext.Current; Hashtable departmentSalary = (H ...

  8. iOS UI_APPEARANCE_SELECTOR

    iOS后属性带UI_APPEARANCE_SELECTOR 可以统一设置全局作用 例如: 1>开关控件 @property(nullable, nonatomic, strong) UIColo ...

  9. C# 缩略图算法

    代码写多了,有些使用过的方法和技巧会一时半会想不起来,平日记录下来,方便自己和有需要的人日后查阅. using (var stream = new FileStream(physicalPath, F ...

  10. linux定时任务crond那些事!

    1.定时任务crond介绍 1.1 crond是什么 crond是linux系统中用来定期执行命令或指定程序任务的一种服务或软件. 特殊需求:(秒级别)crond服务就无法搞定了,一般工作中写脚本守护 ...