Page tree
Skip to end of metadata
Go to start of metadata

Purpose

The purpose of this page is to show a step by step approach using Python code to publish data to and receive data from ESP.

Overview

ESP does not have a Python API.  Customers may have Python code which they want to use to publish data to ESP. The way to do it is using the ESP CSV Socket Server Adapter and opening a client socket connection from the Python code and publishing data to ESP in CSV format.

Publishing data from Python to ESP

The code below is tested with Python 2.7 and ESP 5.1 SP04. Create an ESP project and attach the CSV Socket Server Input Adapter to the stream.

CREATE INPUT STREAM NEWSTREAM SCHEMA (Column1 INTEGER,Column2 string,Column3 float);

ATTACH INPUT ADAPTER Socket_CSV_Input1 TYPE dsv_sockin_in TO NEWSTREAM

PROPERTIES

port = 8886,

expectStreamNameOpcode = true;

Compile and run the ESP project, open the stream view for NEWSTREAM.

 

Install Python2.7 and open the Python GUI IDE. Open a new file and copy the code below:

#client example “clientsend.py”

import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

client_socket.connect(('localhost', 8886))

message = 'NEWSSTREAM,i,1,test,10.5\n'

while 1:

    data = client_socket.send(message)

    print "characters send" ,data

    data = raw_input ( "SEND( TYPE q or Q to Quit):" )

    if (data <> 'Q' and data <> 'q'):

            print "resend message"

    else:

        client_socket.close()

        break;

This Python code opens a client socket and connects to the port 8886 and sends CSV formatted data to the ESP stream.

Open a command prompt and navigate to the folder Python is installed and type:

python  clientsend.py

The data gets published to the ESP stream NEWSTREAM.

Subscribing from ESP using Python

In the same project add a CSV Socket Server Output Adapter:

CREATE INPUT STREAM NEWSTREAM SCHEMA (Column1 INTEGER,Column2 string,Column3 float);

ATTACH INPUT ADAPTER Socket_CSV_Input1 TYPE dsv_sockin_in TO NEWSTREAM

PROPERTIES

port = 8886,

expectStreamNameOpcode = true;

ATTACH OUTPUT ADAPTER Socket_CSV_Output1 TYPE dsv_sockin_out TO NEWSTREAM PROPERTIES

PrependStreamNameOpcode = FALSE ,

port = 9996;

Compile and run the ESP project.

 

Create a clientreceive.py file using the Python GUI with the code below

#client example clientreceive.py

import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

client_socket.connect(('localhost', 9996))

while 1:

    data = client_socket.recv(512)

    print "RECEIVED:" , data   

 

The Python code opens an client socket and connects to port 9996 and receives data from ESP.

Open another command prompt and type:

python clientreceive.py

The ESP project in this page uses the deprecated CSV Socket Adapter, as the new toolkit CSV Socket Adapter released in ESP 5.1SP03 does not have server functionality. Another option is to use the SocketServerExample built with ESP's toolkit adapter.

Related Content

Related Documents

ESP socket server adapter

Related SAP Notes/KBAs

 

__________________________________________________________________________________________________________

Use this structure to help you compose your contributions for WIKI and at the same time will ensure spelling and grammar.

1 Comment

  1. Anonymous

    I like your blog, I read this blog please update more content on python, further check it once at  python online training