Quantcast
Channel: Scripting Languages
Viewing all articles
Browse latest Browse all 68

How to use actual SAP NetWeaver RFC Library with Python - The Connection

$
0
0

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:

 

#-Begin-----------------------------------------------------------------

 

#-Packages--------------------------------------------------------------

from ctypes import *

 

#-Structures------------------------------------------------------------

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

 

class RFC_CONNECTION_PARAMETER(Structure):

  _fields_ = [("name", c_wchar_p),

              ("value", c_wchar_p)]

 

#-Main------------------------------------------------------------------

ErrInf = RFC_ERROR_INFO; RfcErrInf = ErrInf()

ConnParams = RFC_CONNECTION_PARAMETER * 5; RfcConnParams = ConnParams()

 

SAPNWRFC = "sapnwrfc.dll"

SAP = windll.LoadLibrary(SAPNWRFC)

 

#-Prototypes------------------------------------------------------------

SAP.RfcOpenConnection.argtypes = [POINTER(ConnParams), c_ulong, \

  POINTER(ErrInf)]

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)

 

else:

  print(RfcErrInf.key)

  print(RfcErrInf.message)

 

#-End-------------------------------------------------------------------

 

0101_Connect.jpg

 

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

 

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

#-Begin-----------------------------------------------------------------

 

#-Include---------------------------------------------------------------

FileName = "sapnwrfc.py"

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

 

#-Import----------------------------------------------------------------

import sys

import platform

 

#-Main------------------------------------------------------------------

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) + " " + \

  str(platform.architecture()[0]))

 

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

  str(platform.release()) + " " + str(platform.machine()))

 

#-End-------------------------------------------------------------------

 

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.

 

Cheers

Stefan


Viewing all articles
Browse latest Browse all 68

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>