简体   繁体   English

如何从Python调用RPG程序

[英]How to call an RPG program from Python

How do you call an RPG program from IBM's Python for PASE? 您如何从IBM的Python for PASE调用RPG程序?

I tried os.system, subprocess.popen/call but they didn't work. 我尝试了os.system,subprocess.popen / call,但它们没有起作用。

Update 更新资料

I found something on IBM's site. 我在IBM网站上找到了东西。 I tried the below; 我尝试了以下内容; it does not give me any error but the RPG program does not execute (meaning it does not give expected result). 它没有给我任何错误,但是RPG程序没有执行(意味着它没有提供预期的结果)。

from itoolkit import * 
from itoolkit.lib.ilibcall import * 

itool = iToolKit()                                       
itransport = iLibCall()                                  
itool.add(iCmd('CALL', 'CALL TEMPLIB/TEMPPGM'))      
itool.call(itransport)    

我不是Python方面的专家,但可以在Young i Professional网站上查看iPgm

This answer assumes you are using iSeriesPython . 该答案假定您正在使用iSeriesPython It does not apply if you are using one of the Python distributions from IBM. 如果您使用的是IBM的Python发行版之一,则该规则适用。

First of all, os.system should work. 首先, os.system应该工作。 It just passes a command to the command line, so you can do 它只是将命令传递到命令行,因此您可以执行

import os
os.system('CALL TEMPLIB/TEMPPGM')

And it should work. 它应该工作。

But, just like any program you call on the command line, you can't receive parameters back that way. 但是,就像在命令行上调用的任何程序一样,您无法以这种方式接收参数。 If you need to get the value of output parameters, use os400.Program to call any *PGM object. 如果需要获取输出参数的值,请使用os400.Program调用任何* PGM对象。

The documentation isn't the most thorough or up-to-date, but it tells you what you need to know. 文档不是最详尽或最新的,但它告诉您需要了解的内容。 In this case, since your example doesn't have parameters, I'm not including parameters in mine: 在这种情况下,由于您的示例没有参数,因此我的参数不包括在内:

import os400
pgm = os400.Program('TEMPPGM', 'TEMPLIB', ())
pgm()

Normally you would want parameters, though. 通常,您通常需要参数。 Here's the example from the documentation: 这是文档中的示例:

getprice = os400.Program('GETPRICE', '*LIBL', (('c', 10), ('d', 9, 0)))
getprice('PART_A', 0)
price = getprice[1]

The docs for the XMLSERVICE-based Python itoolkit are here , specifically you want iPgm . 基于XMLSERVICE的Python itoolkit的文档在这里 ,特别是您需要iPgm

A simple example with no parameters would be: 一个没有参数的简单示例是:

from itoolkit import * 
from itoolkit.lib.ilibcall import *

itransport = iLibCall()
itool = iToolKit()

itool.add(iPgm('pgmcall','TEMPPGM', {'lib': 'TEMPLIB'}))
itool.call(itransport)

output = itool.dict_out('pgmcall')
if 'success' in output:
    print('Success!')

Another solution for specifying the library is to add an iCmd operation first, which adds TEMPLIB to the library list. 指定库的另一种解决方案是先添加一个iCmd操作,这会将TEMPLIB添加到库列表中。 You can find that and more examples here: http://python-itoolkit.readthedocs.io/en/latest/examples.html 您可以在此处找到更多示例: http : //python-itoolkit.readthedocs.io/en/latest/examples.html

The library XMLSERVICE has a set of demo programs in RPG that are callable and some sample code. XMLSERVICE库在RPG中具有一组可调用的演示程序和一些示例代码。 I just did a session on XMLSERVICE at the PowerUP18 conference and used this example, which does work in 7.2 and the OPS licensed program. 我刚刚在PowerUP18会议上就XMLSERVICE进行了一次会议,并使用了此示例,该示例在7.2和OPS许可程序中有效。

from itoolkit.lib.ilibcall import *
itransport = iLibCall()
from itoolkit import *
# XMLSERVICE/ZZCALL:
#     D  INCHARA        S              1a
#     D  INCHARB        S              1a
#     D  INDEC1         S              7p 4
#     D  INDEC2         S             12p 2
#     D  INDS1          DS
#     D   DSCHARA                      1a
#     D   DSCHARB                      1a
#     D   DSDEC1                       7p 4
#     D   DSDEC2                      12p 2
#      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#      * main(): Control flow
#      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#     C     *Entry        PLIST
#     C                   PARM                    INCHARA
#     C                   PARM                    INCHARB
#     C                   PARM                    INDEC1
#     C                   PARM                    INDEC2
#     C                   PARM                    INDS1
itool = iToolKit()
itool.add(iCmd('chglibl', 'CHGLIBL LIBL(XMLSERVICE)'))
itool.add(
iPgm('zzcall','ZZCALL')
.addParm(iData('INCHARA','1a','a'))
.addParm(iData('INCHARB','1a','b'))
.addParm(iData('INDEC1','7p4','32.1234'))
.addParm(iData('INDEC2','12p2','33.33'))
.addParm(
 iDS('INDS1')
 .addData(iData('DSCHARA','1a','a'))
 .addData(iData('DSCHARB','1a','b'))
 .addData(iData('DSDEC1','7p4','32.1234'))
 .addData(iData('DSDEC2','12p2','33.33'))
 )
)

 # xmlservice
itool.call(itransport)

# output
chglibl = itool.dict_out('chglibl')
if 'success' in chglibl:
 print (chglibl['success'])
else:
  print (chglibl['error'])
exit()

zzcall = itool.dict_out('zzcall')
if 'success' in zzcall:
 print (zzcall['success'])
 print ("    INCHARA      : " + zzcall['INCHARA'])
 print ("    INCHARB      : " + zzcall['INCHARB'])
 print ("    INDEC1       : " + zzcall['INDEC1'])
 print ("    INDEC2       : " + zzcall['INDEC2'])
 print ("    INDS1.DSCHARA: " + zzcall['INDS1']['DSCHARA'])
 print ("    INDS1.DSCHARB: " + zzcall['INDS1']['DSCHARB'])
 print ("    INDS1.DSDEC1 : " + zzcall['INDS1']['DSDEC1'])
 print ("    INDS1.DSDEC2 : " + zzcall['INDS1']['DSDEC2'])
else:
 print (zzcall['error'])
exit()

Getting the library list and syntax correct is probably the hardest part 使库列表和语法正确可能是最难的部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM