通过调用``修改用户密码的示例代码
#从conf文件中获取服务器信息和用户信息
info = sapConfig()
system = [info.ashost(),info.sysnr(),info.client()]
try:
# 连接SAP系统
conn = sapConnection().sap_conn()
# 嗲用BAPI修改用户密码
conn.call('SUSR_USER_CHANGE_PASSWORD_RFC',
BNAME=info.username(),
PASSWORD=info.passwd(),
NEW_PASSWORD='newpassword' # 更改为新密码
)
# Close the RFC connection
conn.close()
print("Success changed user password")
except LogonError:
print('ERROR! Logon error for host {0} system number {1} client {2}'
.format(system[0], system[1], system[2]))
else:
print('Password for host {0} system number {1} client {2} changed'
.format(system[0], system[1], system[2]))
对于启用 组策略或者router的服务器,可以使用如下的方式做连接
from pyrfc import Connection
user = 'user'
passwd = 'secretuser'
saprouter = '/H/192.168.0.140/S/3297'
conn = Connection(user=user, passwd=password,
mshost='CLient',
msserv='192.168.0.140',
sysid='01',
group="SPACE",
saprouter=saprouter,
client=‘900')
示例代码3
#!/usr/bin/env python
from pyrfc import Connection, ABAPApplicationError, ABAPRuntimeError, LogonError, CommunicationError
from ConfigParser import ConfigParser
from pprint import PrettyPrinter
def main():
try:
config = ConfigParser()
config.read('sapnwrfc.cfg')
params_connection = config._sections['connection']
conn = Connection(**params_connection)
options = [{ 'TEXT': "FCURR = 'USD'"}]
pp = PrettyPrinter(indent=4)
ROWS_AT_A_TIME = 10
rowskips = 0
while True:
print u"----Begin of Batch---"
result = conn.call('RFC_READ_TABLE', \
QUERY_TABLE = 'TCURR', \
OPTIONS = options, \
ROWSKIPS = rowskips, ROWCOUNT = ROWS_AT_A_TIME)
pp.pprint(result['DATA'])
rowskips += ROWS_AT_A_TIME
if len(result['DATA']) < ROWS_AT_A_TIME:
break
except CommunicationError:
print u"Could not connect to server."
raise
except LogonError:
print u"Could not log in. Wrong credentials?"
raise
except (ABAPApplicationError, ABAPRuntimeError):
print u"An error occurred."
raise
if __name__ == '__main__':
main()
参考文献
- SAP and Python integration or how to take data from SAP easier / Sudo Null IT News
- BAPI for beginners - Part 2 (rpfact.com)
- Using Python to insert data into SAP with PyRFC | by Rafael Cordeiro | Towards Data Science
- Change of user password via BAPI in Python | SAP Blogs
- Calling SAP RFC modules from Python - what you need to remember - DEV Community