Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member182779
Active Contributor

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.c...!</p><p>!https://weblogs.sdn.sap.com/weblogs/images/48024/Flask_SAP_02.png|alt=|src=https://weblogs.sdn.sap.c...!</p><p>!https://weblogs.sdn.sap.com/weblogs/images/48024/Flask_SAP_03.png|alt=|src=https://weblogs.sdn.sap.c...!</body>

12 Comments