A python library to take control of remote IBM hosts
A Python library that provides an interface to communicate with IBM hosts: send commands and text, receive output (screens). The library provides the means to do what a human can do using a 3270 emulator.
The library is highly customizable and is built with simplicity in mind.
It is written in Python 3, runs on Linux and Unix-like Operating Systems, and relies on the s3270
utility. So it is required to have the s3270
installed on your system and available on your PATH.
The library allows you to open a telnet connection to an IBM host, and execute a set of instructions as you specified them in your python program.
A simple pip command brings the library to your environment:
pip install p3270
NB: Make sure that you're using the python3 version of the pip command.
Import the client class from the library:
from p3270 import P3270Client
Create a client object specifying a configuration file:
my_client = P3270Client(configFile='my-config.cfg')
It is also possible to create a client object with the config options stated in the code.
my_client = P3270Client(hostName='192.168.100.25', path='c:\\wc3270\\', codePage='cp277')
If the s3270 program is downloaded as a .zip file or otherwise fails to be added to the path when installed, it is possible to state the path when creating the P3270Client.
Connect the client, and you're good to go:
if not my_client.connect():
print('Connection failed !')
exit(1)
# Start sending your commands to the host ...
The configuration file is used to set a bunch of parameters about the client and host interaction. The file has the following format:
<parameter> = <value>
Lines starting with a '#' are considered as comments and therefore ignored. For now the parameters are:
localhost
.23
.3278-n
and 3279-n
(n
is the model number and can take the value: 2, 3, 4 or 5). The base model 3278 is a green on black 3270 display, whereas 3279 base model is a color 3270 display. Model numbers are used to specify the screen definition (rows and columns) as follows:Model number | Columns | Rows |
---|---|---|
2 | 80 | 24 |
3 | 80 | 32 |
4 | 80 | 43 |
5 | 132 | 27 |
Default is 3279-2 . |
s3270
version. The list can be obtained by running the following command on your shell:# s3270 -v
Default is cp037
.
An example of configuration file would be:
# Target hostname
hostname = 192.168.100.25
# Port
port = 10023
# Model Name: Default 3279-2
model = 3279-2
# TraceFile
traceFile = client.trace
# LU name to use
#LUName = LUPY0001
# EBCDIC character set to use for the host
codePage = cp037
# The file where all the screens should be saved
screensDir = /home/me/projects/python/mainframe/screens/
One configuration file can be shared by multiple clients.
Once the client object (P3270Client class) is created, the following methods can be used to interact with the host.
connect()
:
disconnect()
endSession()
sendEnter()
sendPF(n)
sendPA(n)
sendBackSpace()
sendBackTab()
sendHome()
sendTab()
sendKeys(keys)
clearScreen()
delChar()
delField()
delWord()
eraseChar()
moveCursorDown()
moveCursorUp()
moveCursorLeft()
moveCursorRight()
moveTo(row, col)
moveToFirstInputField()
sendText(text)
saveScreen(fileName, dataType)
screensDir
of the configuration file. Default: screen
html
getScreen()
printScreen()
isConnected()
readTextAtPosition(row, col, length)
readTextArea(row, col, rows, cols)
readTextAtPosition(row, col, expected_text)
waitForField()
trySendTextToField(text, row, col)
All of the above methods return True
if they succeed, and False
otherwise. The only exceptions:
endSession()
, it terminates the emulation session and returns True
in all cases.readTextAtPosition
, readTextArea
, getScreen
all return the text they read.from p3270 import P3270Client
# Connect and test if connection succeeded or not
if not my_client.connect():
print('Connection failed !')
exit(1)
# Save the home screen to a file called 'home.html'. HTML format is the default.
my_client.saveScreen(fileName='home.html')
# Send user name to the current field (user ID)
my_client.sendText('user1')
# Send TAB key to go to the next field
my_client.sendTab()
# Send the user password to the password field.
my_client.sendText('password1')
# Send Enter key to submit the current screen with field contents
my_client.sendEnter()
# Clear the screen
my_client.clearScreen()
# Send the CICS command 'CEMT INQ TASK' to get running tasks
my_client.sendText('CEMT INQ TASK')
# Submit
my_client.sendEnter()
# Capture the screen
my_client.saveScreen(fileName='tasks.html')
# Go back : PF3 key
my_client.sendPF(3)
# Go back again
my_client.sendPF(3)
# Disconnect from the host
my_client.disconnect()
# End the emulation session
my_client.endSession()
Screens should go to the directory specified in the parameter screensDir
specified in the configuration file.
The library generates some log messages on the file p3270.log. You can adjust th elog level by modifying it directly on the library.
GPLv3. See the LICENSE file.