Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c8397e
commit 93a51ef
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
layout: default | ||
title: 提供XML访问 | ||
--- | ||
|
||
# 提供XML访问 | ||
|
||
### 问题 | ||
|
||
如何在web.py中提供XML访问? | ||
|
||
如果需要为第三方应用收发数据,那么提供xml访问是很有必要的。 | ||
|
||
### 解法 | ||
|
||
根据要访问的xml文件(如response.xml)创建一个XML模板。如果XML中有变量,就使用相应的模板标签进行替换。下面是一个例子: | ||
|
||
$def with (code) | ||
<?xml version="1.0"?> | ||
<RequestNotification-Response> | ||
<Status>$code</Status> | ||
</RequestNotification-Response> | ||
|
||
为了提供这个XML,需要创建一个单独的web.py程序(如response.py),它要包含下面的代码。注意:要用"web.header('Content-Type', 'text/xml')"来告知客户端--正在发送的是一个XML文件。 | ||
|
||
|
||
import web | ||
|
||
render = web.template.render('templates/', cache=False) | ||
|
||
urls = ( | ||
'/(.*)', 'index' | ||
) | ||
|
||
app = web.application(urls, globals()) | ||
|
||
class index: | ||
def GET(self, code): | ||
web.header('Content-Type', 'text/xml') | ||
return render.index(code) | ||
|
||
web.webapi.internalerror = web.debugerror | ||
if __name__ == '__main__': app.run() |