/
simpleExampleWithErrors1.py
executable file
·68 lines (54 loc) · 2.46 KB
/
simpleExampleWithErrors1.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
#!/usr/bin/env python3
import xmlrpc.client
host = "localhost"
port = "8080"
urlEndPoint="http://"+host+":"+port+"/users" # Correct endpoint
# Test with the incorrect end point
wrongURLEndPoint="http://"+host+":"+port+"/notcorrect"
testUser = "alec" # Test with valid data
try:
proxy = xmlrpc.client.ServerProxy(wrongURLEndPoint)
result = proxy.userExists(testUser) # Call method with incorrect end point
print("Called userExist() on user {}, result:\n{}".format(
testUser, result))
except xmlrpc.client.Fault as error:
print("called userExit with wrong end point.\nReturn fault is {}".format(
error.faultString))
except xmlrpc.client.ProtocolError as error:
print("""\nA protocol error occurred\nURL: {}\nHTTP/HTTPS headers: {}
Error code: {}\nError message: {}""".format(
error.url, error.headers, error.errcode, error.errmsg))
except ConnectionError as error:
print("\nConnection error. Is the server running? {}".format(error))
proxy = xmlrpc.client.ServerProxy(urlEndPoint) # Now use correct end point
# Pass incorrect arguments to remote procedure
try:
# Call method with incorrect arguments
result = proxy.userExists(testUser, 21)
print("\nCalled userExist() on user {}, result {}".format(testUser, result))
except xmlrpc.client.Fault as error:
print("\ncalled userExit with incorrect args.\nReturn fault is {}".format(
error.faultString))
except xmlrpc.client.ProtocolError as error:
print("""\nA protocol error occurred\nURL: {}\nHTTP/HTTPS headers: {}
"Error code: {}\nError message: {}""".format(
error.url, error.headers, error.errcode, error.errmsg))
# One valid request, one for non existant data
for testUser in ["alec","noOne"]:
try:
result = proxy.userExists(testUser)
print("\nCalled userExist() on user {}, result {}".format(
testUser, result))
result = proxy.getUserAllDetails(testUser)
print("""Called getUserAllDetails() on user {},
UUID is {},
active status is {}""".format(
testUser, result["UUID"], result["activeStatus"]))
except xmlrpc.client.Fault as error:
print("\nCalled users API on user {} failed!\nreason {}".format(
testUser, error.faultString))
except xmlrpc.client.ProtocolError as error:
print("""\nA protocol error occurred
URL: {}\nHTTP/HTTPS headers: {}
Error code: {}\nError message: {}""".format(
error.url, error.headers, error.errcode, error.errmsg))