Skip to content

p5250 is a Python interface to tn5250, acting as a IBM 5250 terminal emulator. It relies heavily on p3270, a IBM 3270 emulator, using s3270 as the actual emulator..

License

simonfaltum/p5250

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Python library to access IBM i / AS400 hosts with a TN5250 emulator.

This is library is built relying on first of all the s3270 utility. It is required to have the s3270 installed on your system - you can learn more here: http://x3270.bgp.nu/ Second, it relies on the P3270 python library. Credit goes to the original developer Mossaab Stiri, his code is available at https://github.com/mstiri/p3270 and it can be found at PyPi. As the P3270 library was a bit behind, I have added a lot of functionality and the updated version can for now be found here: https://github.com/simonfaltum/p3270.

Most of the functionality is the same from the 3270 to the 5250 protocol. For example, moving the cursor, sending text or pressing enter. However, to get access to some of the more advanced functionality in the 5250 protocol, I have used IBM's own keymapping documentation 3270 keyboard mapping for Telnet servers.

Installation

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.

Usage

Import the client class from the library:

from p5250 import P5250Client

Create a client object with the config options stated in the code.

my_client = P5250Client(hostName='localhost', 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. Otherwise, if s3270 is in the path, it is not needed.

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 ...

Library methods:

Once the client object (P3270Client class) is created, the following methods can be used to interact with the host.

  • connect():

    • Description: Connect the client to the host
    • Arguments: none
  • disconnect()

    • Description: Disconenct the client from the host
    • Arguments: none
  • endSession()

    • Description: End the client session
    • Arguments: none
  • sendEnter()

    • Description: Send the Enter key to host
    • Arguments: none
  • sendF(n)

    • Description: Send a F (Function) key - F1, F2, F3... F24
    • Arguments:
      n (int): F key number.
      The number should be in the range 1..24
  • sendBackSpace()

    • Description: Send Back space to the host ()
    • Arguments: none
  • sendBackTab()

    • Description: Send back Tab to the host (go to start of previous input field)
    • Arguments: none
  • sendTab()

    • Description: Send Tab key to the host
    • Arguments: none
  • clearScreen()

    • Description: Clear the screen
    • Arguments: none
  • delChar()

    • Description: Delete character next to the cursor (ASCII DEL)
    • Arguments: none
  • delField()

    • Description: Delete the whole field
    • Arguments: none
  • eraseChar()

    • Description: Erase character previous character (ASCII BS)
    • Arguments: none
  • moveCursorDown()

    • Description: Move cursor down
    • Arguments: none
  • moveCursorUp()

    • Description: Move cursor up
    • Arguments: none
  • moveCursorLeft()

    • Description: Move cursor left
    • Arguments: none
  • moveCursorRight()

    • Description: Move cursor right
    • Arguments: none
  • moveTo(row, col)

    • Description: Move cursor to a specific position
    • Arguments:
      row (int): Row position to which the cursor should be moved.
      col (int): Column position to which the cursor should be moved.
  • moveToFirstInputField()

    • Description: Move cursor to the first input field on the current screen
    • Arguments: none
  • sendText(text)

    • Description: Send text to the host
    • Arguments:
      text (string): The string to send to the host
  • saveScreen(fileName, dataType)

    • Description: Save the current screen to a file
    • Arguments:
      fileName (string): File name to which the screen will be saved.
      If the file does not exist it is created, otherwise it is appended.
      Files are saved under the specified name in the directory specified in the parameter screensDir of the configuration file. Default: screen
      dataType (string): The data type of the captured screen. Supported data types are html, or rtf. Default: html
  • getScreen()

    • Description: Get the actual screen as raw text
    • Arguments: none
  • printScreen()

    • Description: Print the current screen to the standard output
    • Arguments: none
  • isConnected()

    • Description: Get the connection status of the client
    • Arguments: none
  • readTextAtPosition(row, col, length)

    • Description: Reads text at a row,col position and returns it
    • Arguments:
      row (int): Row position on where to read.
      col (int): Column position on where to read.
      length (int): How many chars to read
  • readTextArea(row, col, rows, cols)

    • Description: Reads text area at a row,col position and returns it
    • Arguments:
      row (int): Row position on where to read.
      col (int): Column position on where to read.
      rows (int): Number of rows to read down from the starting row.
      cols (int): Number of columns to read, right from the starting column.
  • readTextAtPosition(row, col, expected_text)

    • Description: Will check at the given coordinates if the text appear or not. Returns true if the text was found, false if not.
    • Arguments:
      row (int): Row position on where to read.
      col (int): Column position on where to read.
      expected_text (string): The text to look for
  • waitForField()

    • Description: Will wait for the field to be ready where the cursor is standing
    • Arguments: none
  • trySendTextToField(text, row, col)

    • Description: Will try and write the given text at the given position. Once the text is written, it will check if the text is now shown at the screen at that position. Returns true if succeeded, false if not.
    • Arguments:
      row (int): Row position on where to read.
      col (int): Column position on where to read.
      text (string): Text to write
  • rollUp()

    • Description: Will roll up a table or menu. This is usually mapped to Page Down.
    • Arguments: none
  • rollDown()

    • Description: Will roll down a table or menu. This is usually mapped to Page Up.
    • Arguments: none

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.

Example:

from p5250 import P5250Client

# 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()

# Go back : F3 key
my_client.sendF(3)

# Go back again
my_client.sendF(3)

# Disconnect from the host 
my_client.disconnect()

# End the emulation session
my_client.endSession()

License

GPLv3. See the LICENSE file.

About

p5250 is a Python interface to tn5250, acting as a IBM 5250 terminal emulator. It relies heavily on p3270, a IBM 3270 emulator, using s3270 as the actual emulator..

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages