cancel
Showing results for 
Search instead for 
Did you mean: 

Python to fetch data from SAP via RFC

Former Member
0 Kudos

Recently, I plan to use Python to fetch from SAP via RFC. I search on internet and choose PyRFC library to fullfill the task. Everything is normal except the connection. The code stops and displays error message when it connects to SAP. Code can be seen below:

import pyrfc

def get_connection(connmeta):
    print('Connecting ...', connmeta['ashost'])
    return Connection(**connmeta)

TEST = { 'user' : 'XX',
'passwd' : 'XX',
'ashost' : 'XXX.XXX.XXX.XXX',
'sysnr' : '00',
'client' : '400',
'lang' : 'EN' }

conn = get_connection(TEST)
ERROR Message<br>
pyrfc._exception.CommunicationError: RFC_COMMUNICATION_FAILURE (rc=1): key=RFC_COMMUNICATION_FAILURE, message=
LOCATION CPIC (TCP/IP) on local host XXX-XXXXXXX with Unicode
ERROR partner 'XXX.XXX.XXX.XXX:3300' not reached
TIME Tue Nov 13 16:33:59 2018
RELEASE 721
COMPONENT NI (network interface)
VERSION 40
RC -10
MODULE nixxi.cpp
LINE 3283
DETAIL NiPConnect2: XXX.XXX.XXX.XXX:3300
SYSTEM CALL connect
ERRNO 10061
ERRNO TEXT WSAECONNREFUSED: Connection refused
COUNTER 2
[MSG: class=, type=, number=, v1-4:=;;;]

It seems issue lies in the server's firewall. AND I googled most answers like this on internet including SAP's community.

Then I telnet port of SAP' s IP and got timeout. It seems closed port causes connection failure.

BUT before using Python, I used VBA to fetch data from SAP. the configure in VBA is almost same as that in Python

Set R3 = CreateObject("SAP.Functions")  
    R3.Connection.System = "PRT"  
    R3.Connection.ApplicationServer = "XXX.XX.XX.XX"  
    R3.Connection.Client = "400"  
    R3.Connection.SystemNumber = "00"  
    R3.Connection.User = "XX"  
    R3.Connection.Password = "XX"  
    R3.Connection.Language = "EN"  
    R3.Connection.Codepage = "1100"  

retcd = R3.Connection.Logon(1, True)  

If retcd <> True Then  
   MsgBox "Logon failure"  
Else  
   MsgBox "Logon success"  
End If

It works fine but with slow efficiency, that's why I choose Python as a workaround.

I'm confused why VBA successes but python fails.

Anyone can solve that for me as I'm stuck here for a few days?

Thanks~

Matt_Fraser
Active Contributor
0 Kudos

Hi Chris,

Welcome to the SAP Community. You'll note that I've changed the tag on your question, as I think it more properly belongs under "NetWeaver ABAP RFC" rather than "Logging of RFC." You can, of course, add additional secondary tags to refine the topic of the question. Unfortunately, I don't think there is one related to Python or VBA.

Best regards,
Matt Fraser
SAP Community Moderator

Accepted Solutions (0)

Answers (0)