1
+ import http .client
2
+ import urllib .parse
3
+ import xml .dom .minidom
4
+
5
+ class soap_consumer :
6
+
7
+ def __init__ (self , msg , json = False ):
8
+ self .msg = msg
9
+ self .json = json
10
+
11
+ def envelope (self ):
12
+ if self .json :
13
+ return self .msg
14
+ else :
15
+ doc = xml .dom .minidom .Document ()
16
+ env = doc .createElement ('soap12:Envelope' )
17
+ env .setAttribute ('xmlns:soap12' , 'http://www.w3.org/2003/05/soap-envelope' )
18
+ env .setAttribute ('xmlns:xsi' , 'http://www.w3.org/2001/XMLSchema-instance' )
19
+ env .setAttribute ('xmlns:xsd' , 'http://www.w3.org/2001/XMLSchema' )
20
+
21
+ #print(self.msg)
22
+
23
+ #XML input request data
24
+ rawdom = xml .dom .minidom .parseString (self .msg )
25
+ messagenode = rawdom .firstChild
26
+
27
+ #Header
28
+ header = doc .createElement ('soap12:Header' )
29
+ env .appendChild (header )
30
+
31
+ #Body
32
+ body = doc .createElement ('soap12:Body' )
33
+ body .appendChild (messagenode )
34
+
35
+ env .appendChild (body )
36
+ doc .appendChild (env )
37
+
38
+ #print(doc.toxml('utf-8'))
39
+
40
+ return doc .toxml ('utf-8' )
41
+
42
+ def send_request (self , url , path , content_type , accept , https = True ):
43
+ data = self .envelope ()
44
+
45
+ #print(data)
46
+ #print(len(data))
47
+
48
+ headers = {"Content-type" : content_type , "Accept" : accept , "Content-length" : len (data )}
49
+ conn = ''
50
+
51
+ if https :
52
+ conn = http .client .HTTPSConnection (url , 443 )
53
+ else :
54
+ conn = http .client .HTTPConnection (url , 80 )
55
+
56
+ #print(conn)
57
+ conn .request ("POST" , path , data , headers )
58
+
59
+ response = conn .getresponse ()
60
+ resp_data = response .read ()
61
+
62
+ #print(resp_data)
63
+
64
+ if response .status == 200 :
65
+ conn .close ()
66
+ return resp_data
67
+ else :
68
+ return 'Failed:' + str (response .status ) + str (resp_data )
69
+
70
+ #XML request body
71
+ swsc = soap_consumer ('<CelsiusToFahrenheit xmlns="https://www.w3schools.com/xml/"><Celsius>36</Celsius></CelsiusToFahrenheit>' )
72
+ resp = swsc .send_request ('www.w3schools.com' , '/xml/tempconvert.asmx' , 'application/soap+xml; charset=utf-8' , 'text/xml' )
73
+ print (resp )
74
+
75
+ #JSON request body
76
+ params = urllib .parse .urlencode ({'@number' : 12524 , '@type' : 'issue' , '@action' : 'show' })
77
+ swsc = soap_consumer (params , True )
78
+ resp = swsc .send_request ('bugs.python.org' , '' , 'application/x-www-form-urlencoded; charset=utf-8' , 'text/plain' )
79
+ print (resp )
0 commit comments