├── README.md
└── tc_interface.py
/README.md:
--------------------------------------------------------------------------------
1 | # teamcenter_python_interface
2 | Teamcenter is a PLM system by Siemens and the vendor exposed the functionality via a set of SOAP API for service consumption. This project connects to Teamcenter via a python program via SOAP API. Because HTTP is a stateless protocol, the JSESSIONID returned by the login SOAP service would be stored for subsequent invocations to Teamcenter to maintain information about who the requestor is.
3 |
--------------------------------------------------------------------------------
/tc_interface.py:
--------------------------------------------------------------------------------
1 | import sys
2 | import requests
3 | import xml.etree.ElementTree as ET
4 |
5 | # Usecase 1: Login to Teamcenter
6 | # ------------------------------
7 | # Python's requests module can be used for leveraging the SOAP calls to Teamcenter.
8 | # Pip install can do the trick of installing the modules if needed. I am using python 2.7
9 | # with the editor as PyCharm Community Version.
10 | url = "https://localhost/tc/services/Core-2006-03-Session?wsdl" # This is the WSDL for login method.
11 | headers = {'Content-Type': 'text/xml', 'SOAPAction': 'login' }
12 | # Understanding SOAP Requests: Different SOA operations have different input structures that are to be populated to make them work.
13 | # The best way to identify this is using SOAPUI. That is a free tool in which you can import the WSDLs of Teamcenter and you can see
14 | # the sample XML request that is prepared for a given SOA operation. You can just copy it and use it in your Python requests.
15 | body = """
16 |
17 |
18 |
19 |
20 | """
21 |
22 | # Ours is HTTPS implementation. So for a quick prototype, I have used the 'verify' as false.
23 | # In practical production grade code, you would have to remove this flag, defaulting it to True.
24 | response = requests.post(url,data=body,headers=headers,verify=False)
25 |
26 | # Understanding SOA Responses: The best way for this one is to just print the 'response.content'. The output would be similar
27 | # to what you typically get in the Teamcenter RAC communication monitor view. How you parse to get the relevant information is
28 | # entirely dependent on the SOA that you are using.
29 | if response.status_code == 200:
30 | print 'Login was successful.'
31 | print response.content
32 | else:
33 | print 'Login failed.'
34 | sys.exit( 'Login operation failed with HTTP Code ' + str( response.status_code ) )
35 |
36 | # HTTP is a stateless protocol. It cannot discern between two requests from the same user.
37 | # Two ways out of this - JWT Tokens or Session Identifiers. Teamcenter uses Session Identifiers
38 | # in the form of JSESSIONID. Upon login operation, a JSESSIONID gets returned in the response.
39 | # Example: JSESSIONID - 8heEOiDBF_4UJ1kq9vWLknb5Kf-aTvyqJSflq3xj.localhost
40 | login_session_id = None
41 | for c in response.cookies:
42 | if c.name == 'JSESSIONID':
43 | login_session_id = c.value
44 |
45 | jsession_token = "JSESSIONID=" + login_session_id
46 | print(jsession_token)
47 |
48 |
49 | # Usecase 2: Get properties of a Document from Teamcenter
50 | # ---------------------------------
51 | url = "https://localhost/tc/services/Core-2006-03-DataManagement?wsdl" # This is the WSDL for getProperies method.
52 | headers = {'Content-Type': 'text/xml', 'SOAPAction': 'getProperties', 'Cookie' : jsession_token }
53 | body = """
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | """
62 | response = requests.post(url,data=body,headers=headers,verify=False)
63 | print response.content
64 | root = ET.fromstring( response.content )
65 |
66 | response_body = root[0]
67 | response_servicedata = response_body[0]
68 | response_dataobjects = response_servicedata[1]
69 |
70 | for objectproperties in response_dataobjects:
71 | if objectproperties.attrib['name'] == 'object_string':
72 | print 'The object_string of the puid is ' + objectproperties.attrib['uiValue']
73 |
74 | if response.status_code == 200:
75 | print 'GetProperties was successful.'
76 | print response.content
77 | else:
78 | print 'GetProperties failed.'
79 | sys.exit( 'GetProperties operation failed with HTTP Code ' + str( response.status_code ) )
80 |
81 | # Usecase 3: Logout from Teamcenter
82 | # ---------------------------------
83 | url = "https://localhost/tc/services/Core-2006-03-Session?wsdl" # This is the WSDL for logout method.
84 | headers = {'Content-Type': 'text/xml', 'SOAPAction': 'logout', 'Cookie' : jsession_token }
85 | body = """
86 |
87 |
88 |
89 |
90 | """
91 | response = requests.post(url,data=body,headers=headers,verify=False)
92 |
93 | if response.status_code == 200:
94 | print 'Logout was successful.'
95 | print response.content
96 | else:
97 | print 'Logout failed.'
98 | sys.exit( 'Logout operation failed with HTTP Code ' + str( response.status_code ) )
99 |
100 |
--------------------------------------------------------------------------------