-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoapport.py
executable file
·146 lines (132 loc) · 4.77 KB
/
soapport.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
#encoding: utf-8
'''
SOAP client
@author: Juan Cañete <[email protected]>
@date: 2013/05/16
'''
import argparse
import uuid
import urllib2
import os,sys
import datetime
import json
import re
from multiprocessing import Pool
XML_TEMPLATE_DIR='./srv'
XML_MAPPINGS_DIR='./srv'
XML_INFO_DIR='./srv'
XML_TEMPLATES={}
XML_MAPPINGS={}
XML_INFO={}
FIELD_SEPARATOR='|'
def request_service(url,data,headers):
init=datetime.datetime.now()
req=urllib2.Request(url,data,headers)
try:
response=urllib2.urlopen(req)
except urllib2.HTTPError, e:
response=e
end=datetime.datetime.now()
stime=end-init
print init.strftime('%Y-%m-%d %H:%M:%S')+'|',
print end.strftime('%Y-%m-%d %H:%M:%S')+'|',
print str(stime.seconds)+'.'+str(stime.microseconds)+'|',
print str(response.code)+'|',
print str(headers['SOAPAction']),
if response.code == 500:
return False
else:
return True
def get_requestinfo(xmlstring,info):
if not XML_INFO.has_key(info):
xmlinfofile=os.path.join(XML_INFO_DIR,info+'.info')
try:
XML_INFO[info]=json.loads(open(xmlinfofile).read().split('\n')[0])
except IOError:
print 'Error opening XML Info File for service: '+info
return None
uri=XML_INFO[info]['__URI__']
service=XML_INFO[info]['__SERVICENAME__']
xmllen=len(xmlstring)
headers={'Content-Type': 'text/xml;charset=UTF-8','SOAPAction': service,'Content-Length': xmllen}
return uri,headers
def get_xmlTradstring(xmlfile):
try:
xmlstr=open(xmlfile).read()
except Exception as e:
print 'Error: opening xml file: '+str(e)
sys.exit()
message_id=str(uuid.uuid4())
xmlstr=xmlstr.replace('__MESSAGEID__',message_id)
return xmlstr
def get_xmlstr_g(file=None,reg=None):
def get_xmlfromreg(reg):
r_fields=reg.split(FIELD_SEPARATOR)
if r_fields is None:
return None
service=r_fields[0]
if not XML_TEMPLATES.has_key(service):
xmltplfile=os.path.join(XML_TEMPLATE_DIR,service+'.tpl')
try:
XML_TEMPLATES[service]=open(xmltplfile).read()
except IOError:
print 'Error opening XML Template file for service: '+service
return None
if not XML_MAPPINGS.has_key(service):
xmlmapfile=os.path.join(XML_MAPPINGS_DIR,service+'.map')
try:
XML_MAPPINGS[service]=json.loads(open(xmlmapfile).read().split('\n')[0])
except IOError:
print 'Error opening XML Mappings file for service: '+service
return None
except ValueError as e:
print 'Error processing XML Mappings file for service: '+service+' '+str(e)
return None
try:
xmlstr=XML_TEMPLATES[service]
for param,position in XML_MAPPINGS[service].iteritems():
xmlstr=xmlstr.replace(param,r_fields[position])
xmlstr=xmlstr.replace('__MESSAGEID__',str(uuid.uuid4()))
except IndexError:
print 'Error replacing service XML template'
return None
else:
return xmlstr
if reg:
xmlstr=get_xmlfromreg(reg)
if xmlstr:
yield xmlstr,reg
elif file:
for line in file:
reg=line.split('\n')[0]
xmlstr=get_xmlfromreg(reg)
if xmlstr:
yield xmlstr,reg
def parallel_request(request):
url,data,headers,reg=request
request_service(url,data,headers)
print reg.split('\n')[0].split(FIELD_SEPARATOR)[1:]
def main():
parser = argparse.ArgumentParser(description='SOAP Motherfucker client')
parser.add_argument('-s','--server', required=True,help='S2T server/IP')
parser.add_argument('-p','--port', required=True, type=int,help='S2T server port')
parser.add_argument('-f','--file', type=argparse.FileType('r'), help='File containing request values')
parser.add_argument('-r','--request', help='Individual request parameters')
parser.add_argument('-c','--concurrency',type=int,default=10,help='Concurrency level')
args = parser.parse_args()
server=args.server
port=args.port
concurrency=args.concurrency
if (args.file and args.request) or (not args.file and not args.request):
parser.print_help()
sys.exit('Error: Need to determine either --file or --request parameter')
p=Pool(concurrency)
requests=[]
for xmlstr,reg in get_xmlstr_g(file=args.file,reg=args.request):
uri,headers=get_requestinfo(xmlstr,reg.split(FIELD_SEPARATOR)[0])
url='http://'+server+':'+str(port)+uri
requests.append((url,xmlstr,headers,reg))
xs=p.map(parallel_request,requests)
if __name__=='__main__':
main()