Skip to Content
Technical Articles

How to use SAP NetWeaver RFC Library with Python – The Connection

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.

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




  • 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




    • 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


      • 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

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



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

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


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


  • Hi Stefan,

    I am trying your approach and having a strange problem. I am using the below software versions.


    MajorVersion: 7500
    MinorVersion: 0
    PatchLevel: 1
    Version: 750 Patch Level 1


    Python version: 3.7.0 64bit
    Operating system: Windows 10 AMD64

    • I am able to successfully register python server with NW Gateway
    • RFC Connection test works as well
    • RFC unicode test works fine as well

    The problem is when I call the Function from ABAP, Registered Python server stops with the below error.

    Traceback (most recent call last):
    File “”, line 85, in <module>
    rc = SAP.RfcListenAndDispatch(hCon, 1, RfcErrInf)
    OSError: exception: access violation writing 0x0000000000000000

    Could you please advise how to fix this? Thank you in advance.

    – Siva.




    • Hello Siva,

      thanks for your comment.

      I don’t understand your question. It seems that you mix different connection types. On the one Hand RFC and on the other hand COM.

      I installed x64 Version 3.7 of Python and the actual PyWin for 3.7 and checked all RFC examples, which runs well, and my test from here, which doesn’t work.

      Python x64 crashes immediately at the registering of the COM server.

      Please post your complete source to give me the possibility to understand your problem.

      SAP GUI for Windows is an x86 application. I don't know how the x64 PyWin registering works. It is possible to use x64 COM server with x86 application, but therefore it is necessary to set some special entries in the registry. If PyWin don't do that you must do that manually. 

      Best regards

      • Hi Stefan,

        To set the correct context, I am referring to your article here. Python as server application. Sorry for the mix up.

        I am using the same code you used in that article. Please advise.

        - Siva.

        • Hello Siva,

          you can't use 64-bit Python with 32-bit SAP GUI for Windows. Please try the test again with 32-bit Python and let us know your results.

          Best regards

          • Hi Stefan,

            I have tried the following combinations without success.

            1. SAP NW RFC SDK 32-bit with Python 3.7.0 32-bit
            2. SAP NW RFC SDK 64-bit with Python 3.7.0 64-bit
            3. SAP GUI 750 Patch level 4 32-bit for windows with Python 3.7.0 32-bit

            All the cases above are giving me the same result and failing with below error when the function is called from ABAP via RFC. I am trying to dig deeper by enabling level 3 trace in the meantime. I will update back on my findings.

            Traceback (most recent call last):
            File “”, line 85, in <module>
            rc = SAP.RfcListenAndDispatch(hCon, 1, RfcErrInf)
            OSError: exception: access violation writing 0x0000000000000000


            - Siva