Skip to Content
Author's profile photo Alvaro Tejada Galindo

Tasting the mix of Python and SAP

t’s been a very long time since my last “Tasting the mix of” blog post…but here we are to change that -;)

Lately, I have been learning Python…a sexy, powerful and easy to learn programming language…so of course…every time I learn something new, I want to apply it to the SAP world…and lucky me, Piers Harding  (http://www.piersharding.com/blog/) had already creating an Python SAPRfc connector  (http://pypi.python.org/pypi/sapnwrfc/) -:D

So, with everything setup and working I proceeded to start working on the SE16 emulator using the DOS screen…but then I realized that even when didn’t do an SE16, my good friend David Hull  (/people/david.hull2/blog) was already using Python, SAPRfc and DOS screen in his blog entitled Python and SAP Adventures  ().

As I had already worked with Micro Frameworks in Ruby, I thought it was a good idea to implement the same using Python…so after my research I found two nice candidates (Take note that I love Micro Frameworks and not so streamed Frameworks…meaning that I don’t like, don’t know and don’t use Rails or Djanjo…I like to keep it simple).

These Micro Frameworks are called Bottle  (http://bottlepy.org/) and Flask  (http://flask.pocoo.org/) (Weird name, huh?).

So, to make things simple, I used the YAML approach, where we use a configuration like file where we are going to put our connection strings…something like the SAPLogon.ini

sap.yml

ashost: localhostsysnr: “00”client: “001”lang: ENtrace: 1loglevel: warn

For the source codes, let’s start with Bottle.

SE16_Bottle.py

SE16_Flask.py<br />

<textarea cols=”70″ rows=”20″>from flask import Flask, redirect, request

import sapnwrfc

app = Flask(__name__)

conn = “”

@app.route(“/”)

def login():

    return ”'<DIV ALIGN=’CENTER’><BR><BR><BR><BR>

                <H1>Python (Flask) & SAP – SE16 Emulator</H1>

                <BR><TABLE BORDER=’1′ BORDERCOLOR=’BLUE’

                     BGCOLOR=’WHITE’>

                <FORM ACTION=’/login_submit’ METHOD=’POST’>

                <TR><TD>User</TD><TD>

                <INPUT TYPE=’TEXT’ NAME=’User’></TD></TR>

                <TR><TD>Password</TD>

                <TD><INPUT TYPE=’PASSWORD’ NAME=’Passwd’></TD></TR>

                <TR><TD COLSPAN=’2′ ALIGN=’CENTER’>

                <INPUT TYPE=’SUBMIT’ value=’Log In’ NAME=’LOG_IN’>

                <INPUT TYPE=’RESET’ value=’Clear’></TD></TR>

                </FORM>

                <TABLE>

              </DIV>”’

@app.route(“/login_submit”, methods=[‘GET’, ‘POST’])

def login_submit():

    global conn

    if request.method == ‘POST’:

        user = request.form[‘User’]

        passwd = request.form[‘Passwd’]

        sapnwrfc.base.config_location = “sap.yml”

        sapnwrfc.base.load_config()

        conn = sapnwrfc.base.rfc_connect({‘user’: user,

                                          ‘passwd’: passwd})

        return redirect(“/choose”)

@app.route(“/choose”)

def choose_table():

    return ”'<CENTER>

                <FORM ACTION=’/show’ METHOD=’POST’>

                <INPUT TYPE=’TEXT’ NAME=’Table’><BR>

                <INPUT TYPE=’SUBMIT’ value=’Show Table’

                 NAME=’Show_Table’>

                </FORM>

              </CENTER>”’

@app.route(“/show”, methods=[‘GET’, ‘POST’])

def show_table():

    global conn

    if request.method == ‘POST’:

        fields = []

        fields_name = []

        table = str(request.form[‘Table’])

        func_disc = conn.discover(“RFC_READ_TABLE”)

        func = func_disc.create_function_call()

        func.QUERY_TABLE(table)

        func.DELIMITER(“|”)

        func.invoke()

        data_fields = func.DATA.value

        data_names = func.FIELDS.value

        long_fields = len(func.DATA())

        long_names = len(func.FIELDS())

        for line in range(0, long_fields):

            fields.append(data_fields[line][“WA”].strip())

        for line in range(0, long_names):

            fields_name.append(data_names[line][“FIELDNAME”].strip())

        output = “<table border=’1′><tr>”

        for line in range(0, long_names):

            field_name = fields_name[line]

            output += “<th> %s </th>” % field_name

        output += “</tr>”

        for line in range(0, long_fields):

            output += “<tr>”

            data_split = fields[line].split(“|”)

            for line in range(0, long_names):

                output += “<td> %s </td>” % data_split[line]

            output += “</tr>”

        output += “</table>”

        return output

        conn.close()

if __name__ == “__main__”:

    app.run()

</textarea></p><p> </p><p>!https://weblogs.sdn.sap.com/weblogs/images/48024/Flask_SAP_01.png|alt=|src=https://weblogs.sdn.sap.com/weblogs/images/48024/Flask_SAP_01.png!</p><p>!https://weblogs.sdn.sap.com/weblogs/images/48024/Flask_SAP_02.png|alt=|src=https://weblogs.sdn.sap.com/weblogs/images/48024/Flask_SAP_02.png!</p><p>!https://weblogs.sdn.sap.com/weblogs/images/48024/Flask_SAP_03.png|alt=|src=https://weblogs.sdn.sap.com/weblogs/images/48024/Flask_SAP_03.png!</body>

Assigned Tags

      12 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Krishnendu Laha
      Krishnendu Laha
      Hello Alvaro,

      It is nice to see this experiment.
      I was thinking to learn Ruby / Python as an add on with ABAP for some time. But really do not know where to start and which one to start?

      As I am reading blog, came to know you know both of the world..

      Could you please give some idea / suggestion?

      Thanks in advance.

      Cheers,
      Krish

      Author's profile photo Alvaro Tejada Galindo
      Alvaro Tejada Galindo
      Blog Post Author
      Thanks for your comment Krish -:)

      Well...I would recommend you to learn Python first (Specially because Ruby borrows a lot from it, so it's better to learn Python first and Ruby later).
      There are many great resources on the web and also many great books to buy. Python has a great community and a lot of projects going on. It's easy to learn, powerful and sexy...so go with Python -;)

      Greetings,
      Blag.

      Author's profile photo Former Member
      Former Member
      We have a client-server system with using SAP server and on client we receive screens from server in format XHTML + VXML + JavaScript and i would to ask what is the best way to manage these screen and to fill data form from our recognition application and to submit back the data ?
      Author's profile photo Alvaro Tejada Galindo
      Alvaro Tejada Galindo
      Blog Post Author
      Are you using Python? Ruby? PHP? Flex? Otherwise...I don't think I can help you...I mostly build example integrations between Scripting Languages and SAP...and have never used XHTML or VXML and JavaScript is not one of my strongest knowledges...

      Greetings,
      Blag.

      Author's profile photo Former Member
      Former Member
      I use C# or i can use Java, but can you tell me were can ask this to have a result ?
      Tx in advance !
      Author's profile photo Alvaro Tejada Galindo
      Alvaro Tejada Galindo
      Blog Post Author
      You could ask on the general Scripting Languages forum http://forums.sdn.sap.com/forum.jspa?forumID=196&start=0

      Greetings,
      Blag.

      Author's profile photo Steve Oldner
      Steve Oldner
      Great stuff!  Got any PyBlag cartoons yet?
      Author's profile photo Alvaro Tejada Galindo
      Alvaro Tejada Galindo
      Blog Post Author
      Not yet...but soon to come -;)

      Greetings,
      Blag.

      Author's profile photo Steve Oldner
      Steve Oldner
      What Py version did you use?
      Author's profile photo Alvaro Tejada Galindo
      Alvaro Tejada Galindo
      Blog Post Author
      Steve:

      I used Python 2.6. On Pier Harding's homepage there a compiled ready to use SAPRfc for Python 2.6 http://www.piersharding.com/download/python/sapnwrfc/

      Greetings,
      Blag.

      Author's profile photo Former Member
      Former Member
      So what is the use case for using Python with SAP?
      Author's profile photo Alvaro Tejada Galindo
      Alvaro Tejada Galindo
      Blog Post Author
      Well, building an SE16 is not the best example of course, but it shows what can be done with some imagination.
      Use case? My friend David Hull wrote "Python and SAP Adventures", he's a Basis, so he need to monitorate multiple servers...instead of checking SAP transaction every now and then, he build a Python script that connects to SAP, read the status of the servers and sends him an email when something goes wrong...now he just can sit and relax...cool, right?

      Greetings,
      Blag.