-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoapServer.py
101 lines (83 loc) · 3.1 KB
/
SoapServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import socket
import psutil
import time
from flask import Flask
from flask_spyne import Spyne
from spyne.model.complex import Iterable
from spyne.model.primitive import Integer, Unicode
from spyne.protocol.soap import Soap11
from suds.client import Client
app = Flask(__name__)
spyne = Spyne(app)
extraCPULoad = 0
reqCount = 0
class AOSProjectServices(spyne.Service):
__service_url_path__ = '/aosprojectservices'
__in_protocol__ = Soap11(validator='lxml')
__out_protocol__ = Soap11()
#String Reverse Service
@spyne.srpc(Unicode, _returns = Unicode)
def stringReverse(str):
global extraCPULoad
global reqCount
extraCPULoad = extraCPULoad + 1
reqCount = reqCount + 1
# time.sleep(3)
reversedString = ''
index = len(str)
while index:
index -= 1
reversedString += str[index]
return 'Server 1 -> ' + reversedString
#Add Service
@spyne.srpc(float,float,_returns = str)
def Add(num1,num2):
global extraCPULoad
global reqCount
extraCPULoad = extraCPULoad + 1
reqCount = reqCount + 1
# time.sleep(1)
return 'Server 1 -> ' + str(num1 + num2)
#Multiply Service
@spyne.srpc(float,float,_returns = str)
def Multiply(num1,num2):
global extraCPULoad
global reqCount
extraCPULoad = extraCPULoad + 1
reqCount = reqCount + 1
# time.sleep(2)
return 'Server 1 -> ' + str(num1 * num2)
#Service to return server load to Load Balancing server
@spyne.srpc(_returns = float)
def ServerLoad():
#return psutil.cpu_percent()
return 10 + extraCPULoad
#Service to find the number of requests handled by web server
@spyne.srpc(_returns = str)
def RequestCount():
return 'Server 1 -> ' + str(reqCount)
if __name__ == '__main__':
# Code to get local ip of the machine
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
localIP = s.getsockname()[0]
#Service Discovery Server URL
serviceDiscoveryServerURL = ['http://'+ localIP +':8082/servicediscovery?wsdl','http://'+localIP+':8083/servicediscovery?wsdl']
#Load Balancing Server URL
loadBalancingServerURL = ['http://'+ localIP +':9005/loadbalancer?wsdl','http://'+localIP+':9006/loadbalancer?wsdl']
#To registers webserver with Service Discovery server
for server in serviceDiscoveryServerURL:
try:
discoveryClient = Client(server,timeout=5)
discoveryClient.service.registerServer("",'http://'+ localIP + ':8000/aosprojectservices?wsdl')
except Exception as identifier:
continue
#To register webserver with Load balancing server
for server in loadBalancingServerURL:
try:
loadBalancerClient = Client(server,timeout=5)
loadBalancerClient.service.registerServer("",'http://'+ localIP + ':8000/aosprojectservices?wsdl')
except Exception as identifier:
continue
#Runs the webserver
app.run(host = '0.0.0.0',port=8000)