Skip to Content
Author's profile photo Former Member

Python and SAP Adventures

I’ve never written a python script before, but I’ve heard comments for years about how much simpler and cleaner it is to code in python than perl.  So, after my previous series of perl blogs, I got inspired to port my perl script to python.  And, thanks to Piers Harding coding the python connector to the SAP NW RFC SDK, this was possible.

Installation was fairly easy.  Of course, running Linux, python was already there.  So I downloaded PyYAML (a requirement for sapnwrfc on python),  uncompressed it, and installed with:

$ tar xvzf PyYAML-3.08.tar.gz 
$ cd PyYAML-3.08
$ sudo python install

Then, I downloaded and uncompressed sapnwrfc for python, and installed it:

$ tar xvzf sapnwrfc-0.06.tar.gz 
$ cd sapnwrfc-0.06
$ sudo python install

And I was off to the races.

But how to code in python?  The first place I looked was the examples that come with sapnwrfc.  Whenever I’m writing a new script, I like to find someone else’s script I can steal copy and modify to save some time.  The example scripts made it clear how to define the connection to my ERP system, as well as the syntax for calling a function module.  So, a short script that executed TH_SERVER_LIST took only a few minutes.

Next, I had to find out how to add the search text.  This was relatively simple, using python support for regular expressions.  Very similar to how perl does it.

After that, a quick google search revealed how to send an smtp alert; copy and paste the code, and – voilà!

Now, I don’t get caught up in all the bickering over which programming language is better, or cooler, or cleaner, or mauve.  I’m sure either one can get the job done quite nicely.  I’ll probably stick with perl, since that’s what I’m used to, but this was a fun exercise.

And with that, here’s the script.  I’m sure it’s not great, as my first and only attempt at python so far, but it’s complete and it works.


import sapnwrfc
import re
import smtplib

sapnwrfc.base.config_location = 'rqa.yml'
conn = sapnwrfc.base.rfc_connect()

searchtext = ['Communication error', 'Error in TemSe Management', 'session.*deleted']
dumptext = ""

### Enumerate the list of available servers
a = fa.create_function_call()
z = a.LIST.value

### Log on to the XMB Interface
b = fb.create_function_call()
y = b.SESSIONID.value
# print "Session ID: ", y

### For each app server, read its system log
for i in range(len(z)):
    c = fc.create_function_call()
    x = c.SYSLOG_TBL.value
    for i in range(len(x)):
        # print x[i]['LINE'].rstrip()
        for s in searchtext:
            if, x[i]['LINE']):
                dumptext = dumptext + x[i]['LINE'].rstrip() + "\n"

### Log off of the XMB Interface
d = fd.create_function_call()


### send alert
if dumptext:
    print "Emailing an alert on the following: \n", dumptext
    SERVER = "localhost"
    FROM = ""
    TO = [""]
    SUBJECT = "Email alert from python"
    TEXT = dumptext
    message = """\
From: %s
To: %s
Subject: %s

""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO, message)


And here it is being executed:image

Assigned tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Alvaro Tejada Galindo
      Alvaro Tejada Galindo

      Thanks a lot for this blog and your Perl series...As I told you, there was a gap concerning blogs on those Scripting come to fill that gap and I'm so thankful about that -:)

      Also, thanks to you, my willing to learn Python and Perl is back again! -:D


      Author's profile photo Mingzuo Shen
      Mingzuo Shen
      You never know when and where python pops up. 🙂
      Author's profile photo Erwin Tenhumberg
      Erwin Tenhumberg
      Hello David,

      Great blog series about scripting languages!!!

      I was planning to do something similar, but I wanted to focus on PHP first. Thus, I'd love to chat with you (via email or on the phone) in order to explore if there are any potential synergies, e.g. if there is anything I could help you with or if there are scenarios we could work on together.

      You should be able to find my email address in my SDN business card. Otherwise I'm sure you will be able to guess my SAP email address!

      Again, great work!!!

      Best regards,