RESTful architecture, is currently a more popular kind of Internet software architecture. REST, the abbreviation for representational state transfer.
The white point is the website is the software, and then the DOT is a service software to support HTTP four ways:
Get is used to retrieve resources, post to create new resources, update resources, put to update resources, and delete to delete resources.
and provide one or more URIs, each of which corresponds to a resource, and the client can use the URI to match the above method and service
Segment of the software interaction. The client is primarily a browser, and the use of RESTful framework software for HTTP support is also convenient for Web applications.
The word rest was put forward by Roy Thomas Fielding in his 2000 doctoral dissertation. He has a lot of contributions,
Can understand. I work in the direction of SDN, is also a relatively tidal dongdong, which floodlight to use the restful framework.
Developers develop some functionality for the software and provide a URI API that allows users to use browsers, curl, and other tools via the provided URI
Get the information you want from the software or set the function of the software.
For the sender, it is to provide the resources corresponding to the URI and URI, and corresponding to them, similar to dicts={'/path? ' : Resource}.
For example, rewrite the HTTP GET method: First obtain the client request URL, parse the URL and then determine its corresponding URI, because of the URI and a resource,
Then the URL will be able to access the resource. The specific implementation of resources is the method or a class, to see the concrete implementation.
Here's a very simple example, because there are a few drawbacks to a truly powerful restful, but as a simple demo,
It should be enough.
Copy Code code as follows:
#-*-coding:utf-8-*-
Import Socket,sys,urllib
From basehttpserver Import *
Class Restful (Basehttprequesthandler): #所有rest的父类
def __init__ (Self,request, client_address, server):
Basehttprequesthandler.__init__ (Self,request, client_address, server)
Self.dp=none
Self.router=none
def basepath (self):
Pass
def getresetlet (self):
Pass
def send (SELF,SRC):
Self.send_response (200)
Self.send_header ("Content-type", "text/html")
Self.end_headers ()
Self.wfile.write (SRC)
Self.wfile.close ()
def done (self):
Self.dp=self.basepath ()
Self.router=self.getrestlet ()
Class Test (Restful): #测试1
def test (self): #这就是一个资源
Return "{\" date\ ": \" 2013-11-19\ "}"
def do_get (self): #重写get方法给了通过客户端请求的url找到对应的资源
Self.done ()
For key in Self.router.keys ():
Tmp=self.dp+key
If TMP in Self.path:
Self.send (Self.router[key] ()) #执行资源
def basepath (self): #这个简单的说就是和下面函数中的路径配合, i.e./wm/time
Return "/WM"
def getrestlet (self): #这儿就是URI与资源对应, there is only the test resource, you can register multiple
rr={}
rr['/time ']=self.test
Return RR
Class Testjson (Restful): #测试2
def Testjson (SELF,VPC,VR): #这里比测试1复杂些, because the value of the parameter needs to be obtained from the URL
src1= "{\" vpc\ ": 1,\" vrouter\ ": 3,\" day\ ": [1,2,3]}"
src2= "{\" vpc\ ": 1,\" vrouter\ ": 4,\" day\ ": [23,21,3]}"
src3= "{\" vpc\ ": 5,\" vrouter\ ": 3,\" day\ ": [13,2,23]}"
TLIST=[SRC1,SRC2,SRC3]
Cmpvpc= "\" vpc\ ":" +vpc
Cmpvr= "\" Vrouter\ ":" +vr
For K in Tlist:
If cmpvpc in K and CMPVR in K:
Return K
def firewall (self):
Return "{\" filter\ ": [\ baid.com/\", \ "c.cn/\"],\ "acl\": {\ "accept\": 123,\ "reject\": 321}} "
def do_get (self): #重写GET, parsing URLs, where the Self.path is similar:/ins/json?vpc=1&vrouter=3
Self.done ()
Print Self.path
If ' vpc ' in Self.path and ' Vrouter ' in Self.path:
Query=none
If '? ' in Self.path:
query = Urllib.splitquery (Self.path)
key=query[0]+ '? '
Param=query[1].split (' & ') #解析获得属性信息, passing to the resource function
pdict={}
For P in Param:
Tmp=p.split (' = ')
PDICT[TMP[0]]=TMP[1]
For k in Self.router.keys ():
If k in key:
Self.send (Self.router[k] (pdict[' vpc '],pdict[' Vrouter ')) #执行资源
Elif ' firewall ' in Self.path:
Self.send (self.router['/firewall ') ()
Else
Self.send ("{}")
def basepath (self):
Return "/ins"
def getrestlet (self):
rr={}
rr['/json? '] =self.testjson #注册资源
rr['/firewall ']=self.firewall
Return RR
Try
Server=httpserver ((', 8084), Testjson) #测试2
Server.serve_forever ()
Except Keyboardinterrupt:
Sys.exit (0)
If you run the above program, run Test 2, run this program to monitor 8084.
Enter http://127.0.0.1:8084/ins/json?vpc=1&vrouter=3 in the browser address bar at this time
Get the following figure, I use the chrome, install plug-ins after the effect.
Here just as a demo, there are many deficiencies in the program. For a good framework, the registration resource should not be in the same class as the resource.
BasePath () and Getrestlet () should not be in the implemented resource class (that is, in the test above), and also make router ineffective.
It should be registered in another class, so that two Tests should be able to run with different URIs, and the Python basehttpserver module used is not suitable for restful.
But that's the way it works, and then there's the time to write a python version of the restful simple framework.