69 lines
1.6 KiB
Python
69 lines
1.6 KiB
Python
import httplib
|
|
import json
|
|
|
|
class StaticFlowPusher(object):
|
|
def __init__(self, server):
|
|
self.server = server
|
|
|
|
|
|
def get(self, data):
|
|
ret = self.rest_call({}, 'GET')
|
|
return json.loads(ret[2])
|
|
|
|
def set(self, data):
|
|
ret = self.rest_call(data, 'POST')
|
|
return ret[0] == 200
|
|
|
|
def remove(self, objtype, data):
|
|
ret = self.rest_call(data, 'DELETE')
|
|
return ret[0] == 200
|
|
|
|
def rest_call(self, data, action):
|
|
path = '/wm/staticflowpusher/json'
|
|
headers = {
|
|
'Content-type': 'application/json',
|
|
'Accept': 'application/json',
|
|
}
|
|
body = json.dumps(data)
|
|
conn = httplib.HTTPConnection(self.server, 8080)
|
|
conn.request(action, path, body, headers)
|
|
response = conn.getresponse()
|
|
ret = (response.status, response.reason, response.read())
|
|
print ret
|
|
conn.close()
|
|
return ret
|
|
|
|
|
|
pusher = StaticFlowPusher('127.0.0.1')
|
|
|
|
flowbe0 = {
|
|
'switch':"01:00:00:00:00:00:00:00",
|
|
"table":"0",
|
|
"name":"flow-0",
|
|
"cookie":"60",
|
|
"priority":"1",
|
|
"active":"true",
|
|
"eth_type":"0x800", #type
|
|
"in_port":"1", #inport
|
|
"ipv4_src":"192.168.2.111", #sip
|
|
"ipv4_dst":"192.168.2.119", #dip
|
|
"actions":"output=2" # 执行动作,输出端口为 2
|
|
}
|
|
flowbe1 = {
|
|
'switch':"01:00:00:00:00:00:00:00",
|
|
"table":"0",
|
|
"name":"flow-1",
|
|
"cookie":"61",
|
|
"priority":"1",
|
|
"active":"true",
|
|
"eth_type":"0x800", #type
|
|
"in_port":"3", #inport
|
|
"ipv4_src":"192.168.2.119", #sip
|
|
"ipv4_dst":"192.168.2.111", #dip
|
|
"actions":"output=2" # 执行动作,输出端口为 2
|
|
}
|
|
pusher.set(flow0)
|
|
pusher.set(flow1)
|
|
|
|
|