Skip to Content

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  ( had already creating an Python SAPRfc connector  ( -: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  ( and Flask  ( (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


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

For the source codes, let’s start with Bottle.<br />

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

import sapnwrfc

app = Flask(__name__)

conn = “”


def login():

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

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

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


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


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


                <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>




@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”


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

                                          ‘passwd’: passwd})

        return redirect(“/choose”)


def choose_table():

    return ”'<CENTER>

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

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

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




@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 =“RFC_READ_TABLE”)

        func = func_disc.create_function_call()




        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):


        for line in range(0, long_names):


        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


if __name__ == “__main__”:

</textarea></p><p> </p><p>!|alt=|src=!</p><p>!|alt=|src=!</p><p>!|alt=|src=!</body>

You must be Logged on to comment or reply to a post.
  • 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.


    • 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 -;)


  • 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 ?
    • 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?