Merge "VNF_Catalogue Codebase"
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / dpi / proxsocket.py
1 #!/bin/env python
2
3 ##
4 ## Copyright (c) 2010-2017 Intel Corporation
5 ##
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
9 ##
10 ##     http://www.apache.org/licenses/LICENSE-2.0
11 ##
12 ## Unless required by applicable law or agreed to in writing, software
13 ## distributed under the License is distributed on an "AS IS" BASIS,
14 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ## See the License for the specific language governing permissions and
16 ## limitations under the License.
17 ##
18
19 import socket
20
21 class ProxSocket:
22     def __init__(self, ip):
23         self._ip = ip;
24         self._dat = ""
25
26         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
27         try:
28             sock.connect((self._ip, 8474))
29         except:
30             raise Exception("Failed to connect to prox on " + self._ip)
31         self._sock = sock;
32
33     def send(self, msg):
34         self._sock.sendall(msg + "\n");
35         return self
36
37     def recv(self):
38         ret_str = "";
39         done = 0;
40         while done == 0:
41             if (len(self._dat) == 0):
42                 self._dat = self._sock.recv(256);
43                 if (self._dat == ''):
44                     return '';
45
46             while(len(self._dat)):
47                 if (self._dat[0] == '\n'):
48                     done = 1
49                     self._dat = self._dat[1:]
50                     break;
51                 else:
52                     ret_str += self._dat[0];
53                     self._dat = self._dat[1:]
54         return ret_str;