The pyrfc Python package provides Python bindings for SAP NetWeaver RFC Library, for a comfortable way of calling ABAP modules from Python and Python modules from ABAP, via SAP Remote Function Call (RFC) protocol.
The pyrfc has been initially built with Python 2.6 and wheels are provided for Python 2.7 and 3.6, on 64 bit Linux and Windows platform.
OS X and ARM platforms are currently not supported either, as SAP NW RFC Library is not available for those platforms.
On Windows platforms the Microsoft Visual C++ 2005 Service Pack 1 Redistributable Package (KB973544), or newer, must be installed, per SAP Note 1375494 - SAP system does not start after applying SAP kernel patch.
To start using pyrfc you need to obtain SAP NW RFC Library from SAP Service Marketplace, following these instructions. The /dist/ folder of this repository contains egg files (.egg) and wheel files (.whl). Either use easy_install to install the appropriate egg file for your system or use pip to install pyrfc from an appropriate wheel file.
A prerequisite to download is having a customer or partner account on SAP Service Marketplace and if you are SAP employee please check SAP Note 1037575 - Software download authorizations for SAP employees.
SAP NW RFC Library is fully backwards compatible, supporting all NetWeaver systems, from today, down to release R/3 4.0. You can therefore always use the newest version released on Service Marketplace and connect to older systems as well.
In order to call remote enabled ABAP function module (ABAP RFM), first a connection must be opened.
>>> from pyrfc import Connection
>>> conn = Connection(ashost='10.0.0.1', sysnr='00', client='100', user='me', passwd='secret')
Using an open connection, we can invoke remote function calls (RFC).
>>> result = conn.call('STFC_CONNECTION', REQUTEXT=u'Hello SAP!')
>>> print result
{u'ECHOTEXT': u'Hello SAP!',
u'RESPTEXT': u'SAP R/3 Rel. 702 Sysid: ABC Date: 20121001 Time: 134524 Logon_Data: 100/ME/E'}
Finally, the connection is closed automatically when the instance is deleted by the garbage collector. As this may take some time, we may either call the close() method explicitly or use the connection as a context manager:
>>> with Connection(user='me', ...) as conn:
conn.call(...)
# connection automatically closed here
Alternatively, connection parameters can be provided as a dictionary, like in a next example, showing the connection via saprouter.
>>> def get_connection(connmeta):
... """Get connection"""
... print 'Connecting ...', connmeta['ashost']
... return Connection(**connmeta)
>>> from pyrfc import Connection
>>> TEST = {
... 'user' : 'me',
... 'passwd' : 'secret',
... 'ashost' : '10.0.0.1',
... 'saprouter' : '/H/111.22.33.44/S/3299/W/e5ngxs/H/555.66.777.888/H/',
... 'sysnr' : '00',
... 'client' : '100',
... 'trace' : '3', #optional, in case you want to trace
... 'lang' : 'EN'
... }
>>> conn = get_connection(TEST)
Connecting ... 10.0.0.1
>>>conn.alive
True
For further details on connection parameters, pyrfc installation and usage, please refer to pyrfc documentation, complementing SAP NW RFC Library programming guide and documentation provided on SAP Service Marketplace.