1. 程式人生 > >Web API學習筆記(Python實現)

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()

 

返回結果: