WSAECONNREFUSED: Connection refused while establishing connection via PyRFC

2.5k Views Asked by At

I chosen PyRFC library to fulfill the task of connecting to SAP from Python. Everything is normal except the connection. The code stops and displays error message when it connects to SAP.

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)

It throws:

ERROR Message

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. Anybody knows why VBA successes but Python fails?

2

There are 2 best solutions below

0
On

This message looks like it is a networking issue:

Connection refused

0
On

"Connection refused" is a standard TCP/IP error message which means that the port number is incorrect. Double check, whether the SystemNumber of your backend system is really "00".