PST SDK  6.0.0.0-272350a
restserver.py
1 """Rest server example of the PST SDK
2 
3 This example shows how to enable the REST server using the PST SDK.
4 The REST server enables network-based access to the PST Tracker using
5 the HTTP protocol. GET and POST requests can be made to the server to
6 send and receive data and change parameters. The REST interface offers
7 a programming language independent interface to the PST Tracker.
8 Besides accessing the REST server directly using a browser, a networking
9 library can be used to interface with the server programatically.
10 
11 Copyright PS-Tech B.V. All Rights Reserved.
12 """
13 import context
14 import time
15 import sys
16 import pstech.pstsdk.tracker as pst
17 import pstech.pstsdk.errors as psterrors
18 
19 """Helper function to register the exit handler with the application"""
20 def register_exit_handler():
21  if sys.platform.startswith("linux"):
22  import signal
23  signal.signal(signal.SIGTERM, exit_handler)
24  signal.signal(signal.SIGHUP, exit_handler)
25  signal.signal(signal.SIGQUIT, exit_handler)
26  signal.signal(signal.SIGINT, exit_handler)
27  elif sys.platform.startswith("win"):
28  import win32api
29  win32api.SetConsoleCtrlHandler(exit_handler, True)
30 
31 """Implement the exit handler to shut-down the PST Tracker connection on application termination."""
32 def exit_handler(*args):
33  pst.Tracker.disable_rest_server()
34  pst.Tracker.shutdown()
35 
36 def main():
37  if(len(sys.argv) < 2):
38  print("\nConfiguration Error: A camera configuration file needs to be specified. This file can be found in the Redist folder of your installation. "
39  "See the documentation of the Python bindings for more information.")
40  exit(0)
41 
42  # Register exit_handler for proper shutdown
43  register_exit_handler()
44 
45  try:
46  # Use Context Manager to prevent improper Tracker shutdown on errors.
47  # Create an instance of the Tracker object using the default configuration path and file names.
48  with pst.Tracker("", "","", sys.argv[1]) as tracker:
49  # Start the tracker server.
50  tracker.start()
51 
52  # Enable the REST server. To check if the REST server is started correctly, browse to
53  # http://localhost:7278/PSTapi/SystemCheck
54  # In order to use 127.0.0.1 as an address on Windows 7, execute the following command
55  # on an elevated command prompt to allow communication to this address:
56  # "netsh http add urlacl url=http://127.0.0.1:7278/ user=EVERYONE listen=yes delegate=no"
57  tracker.enable_rest_server("localhost", "7278")
58 
59  print("PST REST server enabled. See the PST SDK Manual for example commands.")
60 
61  # Wait for one minute before terminating this application.
62  time.sleep(60)
63 
64  # Disable the REST server.
65  tracker.disable_rest_server()
66  except psterrors.TrackerError as err:
67  # Catch TrackerError and print error messages.
68  print(err.message)
69 
70 if __name__ == "__main__":
71  main()