Module saprfc is the container for all of the SAP R/3 connection
and data handling classes. It also pulls in the C extension module
saprfcutil, which handles all the communication with librfc (or librfccm).
The bes way to get started is with an example. This example connects to an SAP R/3 system, and does various calls setting the value of parameters, and then extracting the resulting data after the call.
Note: check the examples directory of this distribution for more code bites.
RFC call - client to R/3
This scenario is for when you want to call from your client program to an SAP R/3 instance.
import saprfc
conn = saprfc.conn(ashost='localhost', sysnr='00', client='000',
user='developer', passwd='developer', trace=1)
conn.connect()
print "am I connected: ", conn.is_connected()
print "sysinfo is: ", conn.sapinfo()
iface = conn.discover("RFC_READ_TABLE")
iface.query_table.setValue("TRDIR")
iface.ROWCOUNT.setValue(10)
iface.OPTIONS.setValue( ["NAME LIKE 'SAPL%RFC%'"] )
conn.callrfc( iface )
print "NO. PROGS: ", iface.DATA.rowCount(), " \n"
print "PROGS DATA RAW: ", iface.DATA.value, " \n"
# get the SAP Data Dictionary structure for TRDIR
str = conn.structure("TRDIR")
# various ways for iterating over the results in an
# interface table
for x in iface.data.value:
print "Doing: " + str.toHash(x)['NAME']
print "PROGS HASH ROWS: "
for i in iface.DATA.hashRows():
print "next row: ", i
conn.close()
R/3 to Registered RFC Server program
This scenario is for when you want to create a server program that an R/3 instance can call via RFC within ABAP.
class reg:
def __init__(self):
self.cnt = 0
import os
def LoopHandler(self, srfc):
self.cnt += 1
print "inside the customer loop handler - iter: %d ...\n" % self.cnt
if self.cnt >= 30:
return -1
return 1
def handler(self, iface, srfc):
data = []
print "COMMAND is: #" + iface.COMMAND.getValue() + "#"
out = os.popen(iface.COMMAND.getValue(), "r")
for row in out:
data.append(row)
out.close()
iface.PIPEDATA.setValue( data )
return 1
if __name__ == "__main__":
import saprfc
conn = saprfc.conn(gwhost='seahorse.local.net', gwserv='3300', tpname='wibble.rfcexec')
cb = reg()
ifc = saprfc.iface("RFC_REMOTE_PIPE", callback=cb)
ifc.addParm( saprfc.parm("COMMAND", "", saprfc.RFCIMPORT, saprfc.RFCTYPE_CHAR, 256))
ifc.addParm( saprfc.parm("READ", "", saprfc.RFCIMPORT, saprfc.RFCTYPE_CHAR, 1))
ifc.addParm( saprfc.tab("PIPEDATA", "", 80))
conn.iface(ifc)
conn.accept(callback=cb, wait=3)
#conn.accept(wait=3)
print "All finished (reg.py) \n"
R/3 to Registered RFC Server program with tRFC
This scenario is for when you want to create a server program that an R/3 instance can call via RFC within ABAP, using transaction (queued) RFC.
class reg:
def __init__(self):
self.cnt = 0
import os
def LoopHandler(self, srfc):
self.cnt += 1
print "inside the customer loop handler - iter: %d ...\n" % self.cnt
if self.cnt >= 300:
return -1
return 1
def handler(self, iface, srfc, tid):
data = []
print "TID (reg.handler) is: #" + tid + "#"
print "COMMAND is: #" + iface.COMMAND.getValue() + "#"
out = os.popen(iface.COMMAND.getValue(), "r")
for row in out:
data.append(row)
out.close()
iface.PIPEDATA.setValue( data )
return 1
def TRFCCheck(self, srfc, tid):
print "TID (TRFCCheck) is: #" + tid + "#"
return False
def TRFCCommit(self, srfc, tid):
print "TID (TRFCCommit) is: #" + tid + "#"
return
def TRFCConfirm(self, srfc, tid):
print "TID (TRFCConfirm) is: #" + tid + "#"
return
def TRFCRollback(self, srfc, tid):
print "TID (TRFCRollback) is: #" + tid + "#"
return
if __name__ == "__main__":
import saprfc
cb = reg()
conn = saprfc.conn(gwhost='seahorse.local.net', gwserv='3300', tpname='wibble.rfcexec', trfc=cb)
ifc = saprfc.iface("RFC_REMOTE_PIPE", callback=cb)
ifc.addParm( saprfc.parm("COMMAND", "", saprfc.RFCIMPORT, saprfc.RFCTYPE_CHAR, 256))
ifc.addParm( saprfc.parm("READ", "", saprfc.RFCIMPORT, saprfc.RFCTYPE_CHAR, 1))
ifc.addParm( saprfc.tab("PIPEDATA", "", 80))
conn.iface(ifc)
conn.accept(callback=cb, wait=5)
print "All finished (trfc_reg.py) \n"