Web API学习笔记(Python实现)

 参考指南:

Web API入门指南

http://www.cnblogs.com/guyun/p/4589115.html

用Python写一个简单的Web框架

http://www.cnblogs.com/russellluo/p/3338616.html

WSGI接口 def application(environ, start_response)

https://blog.csdn.net/tycoon1988/article/details/40394555

Web API :

面向如浏览器,移动设备等各种客户端,提供Http服务的框架。

支持基于HTTP的各种操作(get,post,put,delete)。

请求的回复格式支持JSON,XML,CSV等。

使用场景:

1)服务在http协议之上,利用http协议的各种功能;

2)服务需被各种客户端(尤其是移动客户端)调用。

WISG(Web Server Gateway Interface):

在Python中,WSGI(Web Server Gateway Interface)定义了Web服务器与Web应用(或Web框架)之间的标准接口.

利用WSGI,可以很方便写一个Web框架。

引用方式是:from wsgiref.simple_server import make_server。

application()函数就是符合WSGI标准的一个HTTP处理函数,它接收两个参数:

1)environ:一个包含所有HTTP请求信息的dict对象;

2)start_response:一个发送HTTP响应的函数。

urlparse解析URL参数模块:

可以对URL按照一定格式进行拆分或拼接。

urlparse.parse_qs()方法返回解析URL后的字典

Json(JavaScriptObject Notation, JS 对象标记):

是轻量级的数据交换格式

格式:双引号 "" 包裹健名,使用冒号 : 分隔,然后紧接着值:

如 {"firstName": "Json"}

优点:使用的字符比xml与html等少,大大节约传输数据占用的带宽;

语法格式与层次结构比较清晰,容易阅读。

json.dumps()函数是将字典转化为字符串

实例:

 实例1:启动一个简单web,访问时返回hello world!字符串

# coding:utf-8

#导入WISG(Web Server Gateway Interface)
from wsgiref.simple_server import make_server

#application()函数是Python中符合WSGI标准的一个HTTP处理函数,返回是一个字符串
def application(environ,start_response):
     #start_response如下调用就会发送HTTP响应的Header,注意只能调用一次start_response()函数发送Header。
     #start_response()函数两个参数,一是HTTP响应码,一是一组list表示的HTTP Header,每个Header用一个包含两个str的数组表示
     status='200 OK'
     response_headers = [('Content-type', 'text/plain')]
     start_response(status,response_headers)
     return ['Hello world!\n']

ip='0.0.0.0'
port=8089
httpd =make_server(ip,port,application)
print("server is started, port is 8089....")
httpd.serve_forever()

运行结果:

实例2:启动一个简单web,访问接口,返回解析URL的值

# coding:utf-8

#导入WISG(Web Server Gateway Interface)
from wsgiref.simple_server import make_server
import urlparse

#application()函数是Python中符合WSGI标准的一个HTTP处理函数,返回是一个字符串
def application(environ,start_response):
     #start_response如下调用就会发送HTTP响应的Header,注意只能调用一次start_response()函数发送Header。
     #start_response()函数两个参数,一是HTTP响应码,一是一组list表示的HTTP Header,每个Header用一个包含两个str的数组表示
     status='200 OK'
     response_headers = [('Content-type', 'text/html')]
     start_response(status,response_headers)

     #调用urlparse的parse_qs解析URL参数,并返回字典
     query_args=environ['QUERY_STRING']
     params = urlparse.parse_qs(environ['QUERY_STRING'])

     print(str(params))
     return [str(params)]

ip='0.0.0.0'
port=8089
httpd =make_server(ip,port,application)
print("server is started, port is 8089....")
httpd.serve_forever()

 运行结果:

 实例3:启动一个简单web,访问接口,返回解析URL的值(n个变量),JSON格式

# coding:utf-8

#导入WISG(Web Server Gateway Interface)
from wsgiref.simple_server import make_server
import urlparse
import json

#application()函数是Python中符合WSGI标准的一个HTTP处理函数,返回是一个字符串
def application(environ,start_response):
      #start_response如下调用就会发送HTTP响应的Header,注意只能调用一次start_response()函数发送Header。
     #start_response()函数两个参数,一是HTTP响应码,一是一组list表示的HTTP Header,每个Header用一个包含两个str的数组表示
     status='200 OK'
     response_headers = [('Content-type', 'text/html')]
     start_response(status,response_headers)

     #调用urlparse的parse_qs解析URL参数,并返回字典
     query_args=environ['QUERY_STRING']
     params = urlparse.parse_qs(environ['QUERY_STRING'])
     #返回的字段,需要转换为字符串作为函数的输出
     print(str(params))
     #json.dumps()函数是将字典转化为字符串
     result=json.dumps(params)
     return [result]

ip='0.0.0.0'
port=8089
httpd =make_server(ip,port,application)
print("server is started, port is 8089....")
httpd.serve_forever()

返回结果:

猜你喜欢

转载自www.cnblogs.com/xiaoer/p/10180510.html
今日推荐