Skip to Content

Hello community,

I am not a Python programmer, but this week I had a discussion with a Python programmer about the using of actual SAP NetWeaver RFC library with Python. He told me, that he uses the library pysaprfc, which bases on the classic librfc32.dll. So I am a little bit surprised about his decision, because on the one hand Python offers via ctypes a complete interface to use the actual SAP NetWeaver RFC library. On the other hand is the classic RFC library (a little bit) outdated, e.g. it doesn’t supports all data types – look here in the ActiveX context. I am searching in the internet and I am surprised again, I can’t find an example snippet or something else. This was my launch to create the following examples.


The first example shows how to connect an SAP system:


from ctypes import *

class RFC_ERROR_INFO(Structure):
  _fields_ = [("code", c_long),
              ("group", c_long),
              ("key", c_wchar * 128),
              ("message", c_wchar * 512),
              ("abapMsgClass", c_wchar * 21),
              ("abapMsgType", c_wchar * 2),
              ("abapMsgNumber", c_wchar * 4),
              ("abapMsgV1", c_wchar * 51),
              ("abapMsgV2", c_wchar * 51),
              ("abapMsgV3", c_wchar * 51),
              ("abapMsgV4", c_wchar * 51)]

  _fields_ = [("name", c_wchar_p),
              ("value", c_wchar_p)]

ErrInf = RFC_ERROR_INFO; RfcErrInf = ErrInf()
ConnParams = RFC_CONNECTION_PARAMETER * 5; RfcConnParams = ConnParams()

SAPNWRFC = "sapnwrfc.dll"
SAP = windll.LoadLibrary(SAPNWRFC)

SAP.RfcOpenConnection.argtypes = [POINTER(ConnParams), c_ulong, \
SAP.RfcOpenConnection.restype = c_void_p
SAP.RfcCloseConnection.argtypes = [c_void_p, POINTER(ErrInf)]
SAP.RfcCloseConnection.restype = c_ulong

#-Connection parameters-------------------------------------------------
RfcConnParams[0].name = "ASHOST"; RfcConnParams[0].value = "ABAP"
RfcConnParams[1].name = "SYSNR" ; RfcConnParams[1].value = "00"
RfcConnParams[2].name = "CLIENT"; RfcConnParams[2].value = "001"
RfcConnParams[3].name = "USER"  ; RfcConnParams[3].value = "BCUSER"
RfcConnParams[4].name = "PASSWD"; RfcConnParams[4].value = "minisap"

hRFC = SAP.RfcOpenConnection(RfcConnParams, 5, RfcErrInf)
if hRFC != None:

  windll.user32.MessageBoxW(None, "Check connection with TAC SMGW", \
    "", 0)

  #- Check connection with TAC SMGW in the SAP system

  rc = SAP.RfcCloseConnection(hRFC, RfcErrInf)





The second example shows how to get the version number of the library:

# -*- coding: iso-8859-15 -*-

FileName = ""
exec(compile(open(FileName).read(), FileName, "exec"))

import sys
import platform

MajorVersion = c_ulong(0)
MinorVersion = c_ulong(0)
PatchLevel = c_ulong(0)
Version = SAP.RfcGetVersion(MajorVersion, MinorVersion, PatchLevel)

print("MajorVersion: " + str(MajorVersion.value))
print("MinorVersion: " + str(MinorVersion.value))
print("PatchLevel: " + str(PatchLevel.value))
print("Version: " + Version)

del SAP

PyVer = sys.version_info
print("\nPython version: " + str(PyVer.major) + "." + \
  str(PyVer.minor) + "." + str(PyVer.micro) + " " + \

print("Operating system: " + str(platform.system()) + " " + \
  str(platform.release()) + " " + str(platform.machine()))


As you can see I am a newbie in Python, but I think this snippets are a good precondition to intensify the using of actual SAP NetWeaver RFC library with Python directly. I developed this examples with the actual Python version 3.4.1, in an Windows environment with the actual SAP NetWeaver RFC library 720 PL24.

2015/09/16: Three days ago the new Python release 3.5.0 has been published. The method above works perfect with the new release.

2017/04/24: I check the method above with Python release 3.6.1 x64 and with the SAP NetWeaver RFC library 721 PL42 x64 and it works perfect.


To report this post you need to login first.


You must be Logged on to comment or reply to a post.

  1. Sanjayan Santhana ganesan

    Hi Stefan/Srdjan,

    Can you please  explain the same with Python RFC connector.

    My requirement is :

    It should be a simple python script which connects to ABAP function module(RFC enabled) and gets data directly into HANA system using looping logic.

    I have the import parameters and export parameters . But im not sure how to call the ABAP FM using Pyrfc  connector. The syntax itself is confusing.




  2. John Conte

    Hi Stefan!


    Thank you for the  post.  This is really great. 🙂

    I am trying to use your source code for RFC_SySTEM_INFO, but I get an issue with the SAPNWRFC.dll.  I changed the code to point to where the dll was, but it is complaining that SAPNWRFC.DLL is not a valid Win32 application.

    I am on a 64 bit Windows 10 pro OS.  Any ideas?    Thank you!


    OSError                                   Traceback (most recent call last)
    <ipython-input-2-1fb4094919c5> in <module>()
         31 SAPNWRFC = r"C:\Program Files\SAP\SAPGUI750\System\sapnwrfc.dll"
    ---> 32 SAP = windll.LoadLibrary(SAPNWRFC)
         34 #-Prototypes------------------------------------------------------------
    ~\Anaconda3\lib\ctypes\ in LoadLibrary(self, name)
        425     def LoadLibrary(self, name):
    --> 426         return self._dlltype(name)
        428 cdll = LibraryLoader(CDLL)
    ~\Anaconda3\lib\ctypes\ in __init__(self, name, mode, handle, use_errno, use_last_error)
        347         if handle is None:
    --> 348             self._handle = _dlopen(self._name, mode)
        349         else:
        350             self._handle = handle
    OSError: [WinError 193] %1 is not a valid Win32 application




    1. Stefan Schnell Post author

      Hello John,

      thank you for your reply. If you use x64 version of Python you must also use x64 version of SAPNWRFC.dll. It is not possible to use the x86 version, but the SAP GUI for Windows 7.50 delivers only the x86 version, because it is an x86 application. You must download the correct version from the SAP Support Portal > Support Packages and Patches > By Category >  SAP Connectors > SAP NW RFC SDK > SAP NW RFC SDK 7.50, choose Windows on X64 64Bit and download


      1. John Conte

        Thank you Stefan for your quick response.  For some reason, I was under the impression that my SAPGUI 750 install was x64.  I will install the NWRFC x64 SDK as you recommend and try it.

        Thanks again – John

        1. John Conte

          Hi Stefan,

          just wanted to feedback that it worked! 🙂

          Thank you again.

          I did have an issue at first because my SAP system is behind a SAProuter and the examples I had seen did not show how to handle it.  I eventually figured it out:

          ASHOST = /H/<saprouter_ip>/S/<port>/H/<sap_app_server_ip or hostname>



  3. John Conte

    I failed to mention I am running Running python 3.6.3 (Anaconda 64bit) , under SAPGUI750.

    I tried to edit my last post  but received authentication errors…

  4. John Conte

    I did do some homework and found this:

    but I did not install the RFC dll via SAPCAR – it was part of the standard 750 install.  Thanks for any guidance here.


    1. Stefan Schnell Post author

      Hello John,

      with release 7.20 of the SAPNWRFCSDK you need SAPCAR, but with the actual release 7.50 it is not longer necessary. The package container is in ZIP format. As I wrote above, you need the x64 version, which is not available with a SAP GUI for Windows installation.



Leave a Reply