Home
Categories
Dictionary
Glossary
Download
Project Details
Changes Log
What Links Here
FAQ
License

Python http tutorial



In the first Python tutorial, one of the modules was reimplemented in the Python scripting language, using UDP sockets. In this tutorial, we will reimplement the Python module using http communication.

The code of this tutorial is in the samples/python/sample7 directory.

Overview

In the first Python tutorial, one of the module was implemented in Python:
  • The PublishModule module increments or decrements a value cyclically
We will change the PublishModule by communicating though http rather than UDP.

Generating the Python library

We will generate the two following scripts by the Browser:
  • The pythonHttpModule.py script
  • The pythonHttpUtils.py script
These two supporting scripts do not depend on the content of the Python script and can be retrieved in the Browser by performing the command Tools => Generate Python Library. You must check the checkbox to produce these modules rather than the default ones used for UDP Python modules.

XML configuration

Only the applications.xml XML file will be modified to use a Python http module.

Applications definition

We will change the applications.xml XML file, by modifying the PublishModule configuration:
      <application name="publishAppli">
         <modules>
            <pythonHttpModule name="PublishModule">
               <pythonImplementation path="pythonAppli" port="8080"/>
               <interfaces>
                  <eventReceived service="event"/>
                  <cyclic service="published"/>
               </interfaces>
            </pythonHttpModule>
         </modules>
      </application>

Code the PublishModule in python

The Python code is different of the Python code used for the first Python tutorial because we will implement the start method to start a timer.

As we are communicating through http, the Python module must make a notify request to get the value of the event service. We will start a timer in the start method to call the tick method in a loop.


      from pythonHttpUtils import PythonHttpUtils

      class PythonAppli:
         step = 1
         count = 0

         def start(self):
           # start a timer with a period of 1 second, and call the tick method for each iteration
           self.pythonHttpUtils.startTimer('timer', 1, self.tick)

         def subscribe(self, serviceName):
           eventService = self.pythonHttpUtils.getService(serviceName);
           eventValue = eventService["event"]
           if eventValue:
             self.step = -1
           else:
             self.step = 1

         def tick(self):   
           # this method is called for each iteration of the timer  
           self.pythonHttpUtils.notify("event") # used to get the content of the "event" service
           self.subscribe("event") # update the event attribute

           # invoke the service
           publishService = self.pythonHttpUtils.getService("published");
           self.pythonHttpUtils.setValue(publishService, "value", self.count)
           r1 = self.pythonHttpUtils.invoke("published")
           # update the count value
           self.count = self.count + self.step

List of available routes

The following routes are available for the Python script:
  • GET http://127.0.0.1:8080/proto/api/notify/event to get the content of the event service
  • GET http://127.0.0.1:8080/proto/api/notify/published to get the content of the published service
  • POST http://127.0.0.1:8080/proto/api/invoke/published to invoke the published service
  • POST http://127.0.0.1:8080/proto/api/stdout to send a message to the framework Logging
  • POST http://127.0.0.1:8080/proto/api/stderr to send an error message to the framework Logging
Among these routes, only these ones are used in this tutorial by our Python script:
  • GET http://127.0.0.1:8080/proto/api/notify/event to get the content of the event service
  • POST http://127.0.0.1:8080/proto/api/invoke/published to invoke the published service

Start the framework

After starting the framework, we will see the same result as for the first Python tutorial.

Note that it is perfectly possible to send requests through a web browser for a particular route. For example, if you open your web browser at the http://127.0.0.1:8080/proto/api/notify/event address on a tab, you will obtain the following result:
pythonhttpget

See also


Categories: tutorials

Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence