python5250
ibm5250 is a pure-Python library for automating IBM 5250 terminal sessions
against IBM i (AS/400) systems over TN5250/TN5250E. It speaks the Telnet
negotiation, 5250 data stream, and screen/field model directly — no green-screen
emulator or external dependencies required — so you can script sign-on,
navigation, and data entry against a 5250 host from plain Python.
See src/ibm5250/data-stream-logic.md for
a detailed walkthrough of how the library parses the wire protocol into screen
state.
Installation
pip install ibm5250
Requires Python >= 3.11.
Usage
from ibm5250 import Terminal, AID
with Terminal("as400.example.com") as t:
t.wait_for_text("Sign On")
t.find_field_by_label("User").value = "MYUSER"
t.find_field_by_label("Password").value = "SECRET"
t.send_key(AID.ENTER)
t.wait_for_text("Main Menu")
print(t.screen.get_screen_text())
send_key blocks until the host has finished replying (the keyboard unlocks),
however many records that takes.
Connection options
Terminal(
"as400.example.com",
port=992,
use_tls=True, # certificate and hostname are verified by default
timeout=10.0, # deadline for send_key / wait_for_* / connect (seconds)
socket_timeout=30.0, # connect, negotiation and per-read socket limit
device_name="MYDEV", # sent as the DEVNAME NEW-ENVIRON user variable
terminal_type="IBM-3179-2", # 24x80 colour; default IBM-3477-FG (27x132 mono)
environ={"CODEPAGE": "37", "CHARSET": "697"}, # extra RFC 4777 user variables
)
- Device name: the host assigns one (e.g.
QPADEV0008) unless you passdevice_name=. - TLS: pass your own
tls_context=(e.g. with a private CA loaded), ortls_verify=Falseto skip certificate checks (insecure; logs a warning). - Screen size follows the host: Clear Unit sets 24×80 or 27×132, as the terminal type allows.
Keys and errors
send_key(aid)sends the cursor position and the field data the host's read command asks for. Print and Record Backspace send no field data.send_attn()andsend_sysreq()send Attn and System Request (header flags, not AID keys), sosend_key(AID.ATTN)/send_key(AID.SYSREQ)raiseValueError.- When the host shows an operator error on the status line (Write Error Code),
send_keyand everywait_for_*raiseOperatorError. The host text is inerr.message(it is kept out ofstr(err)). Callreset()— the Reset key — to clear it before sending another key. - Writing text longer than a field raises
FieldOverflow, and characters the code page (CCSID 37) can't encode raiseEncodingError; nothing is written. type_intoreturnsTruewhen filling an Auto-Enter field sent Enter.- Every
wait_for_*method acceptsfail_on_timeout=Falseto returnFalseinstead of raisingScreenWaitTimeout. - A
FieldProxy(fromfind_field_by_labeletc.) finds its field again by position when the host redraws the screen, and raisesFieldNotFoundif the field is gone.
Capturing host traces
import logging
logging.basicConfig(level=logging.DEBUG)
Terminal("as400.example.com", log_payloads=True)
log_payloads=True logs each record's 5250 data in hex at DEBUG level
(← data … / → data …). Outgoing records that may contain a non-display
(password) field are never logged. Recorded traces can be replayed as tests:
see src/ibm5250/tests/test_screen_build.py for the fixture format.
Upgrading from 0.1.x
These changes can break existing code.
Behaviour
- No default device name: it was
PYTERM01; passdevice_name="PYTERM01"to keep it. It is now actually sent to the host (as theDEVNAMENEW-ENVIRON user variable); before, it was silently ignored by IBM i. - TLS verifies certificates by default: pass
tls_verify=False(or atls_context) for hosts with self-signed certificates.tls_contextwithoutuse_tls=TrueraisesValueError. timeoutis the wait deadline (default now 10 s, was 1 s); the socket usessocket_timeout(default 30 s).send_keywaits for the keyboard to unlock instead of reading one record, and raisesScreenWaitTimeoutif it stays locked.connect()no longer raises when the first screen is slow to arrive.- Over-long field text raises
FieldOverflowinstead of being truncated, and shorter text now replaces the whole previous field content. Fields that span several rows (such as the IBM i command line) take their full length. - Modified fields stay modified after a key is sent, until the host resets them (CC1), as on a real 5250. A redisplayed screen therefore sends the previously typed values again.
Terminal.all_fields()/Screen.get_all_field_values()labels are the text between the previous field and the field, stripped.wait_for_textno longer prints the screen.
API
Terminal(rows=, cols=, poll_interval=, **conn_kwargs)are gone; the connection options are explicit keyword arguments (see above).Terminal.send_raw(data)sends data as the whole record body (row, col, AID, …), with optionalopcode=/flags=; it no longer adds a cursor/AID prefix.- Removed:
KeyboardLocked,FieldResponse, the module-levelbuild_aid_response/build_save_screen_response,decode_save_screen_image/decode_save_screen_fields,Screen.from_save_buffer,Screen.last_aid,Field.data_pos,Field.writable_length,TN5250EHeader,TN5250ERecordType,TN5250EDataType,TN5250ESubNeg,TelnetOpt.TN5250E,AID.NO_AID,ADDR_ENCODE/ADDR_DECODEand theuse_tn5250econnection option. TN5250Connectionsends records withsend_record(data, *, opcode, flags=0, sensitive=False); thesend_*_responsehelpers andtn5250e_activeare gone (usebuild_record,RecordOpcodeandRecordFlag).Field(start_pos, field_format_word, field_control_words)is nowField(start, length, ffw, fcws, attribute):startis the first data position (was the attribute position),ffwis anintandfcwsa tuple ofints.Screen.last_start_of_headeris nowScreen.header.DataStreamParser()takes no arguments andparse(data, *, opcode=...)returns a list;DSEventandDSEventTypewere redesigned around 5250 commands (CLEAR_UNIT,WRITE_TO_DISPLAY,READ,SAVE_SCREEN, …).CC1/CC2values were wrong and are replaced (e.g.CC2.UNLOCK_KEYBOARD,CC2.ALARM,CC1.RESET_MDT_ALL);QueryReply.ScreenSizemodel values andQueryReply.Capabilitieswere corrected;FCW.CONTINUATION_BITis nowFCW.CONTINUED_FIRST/_MIDDLE/_LAST.- The table helpers are public:
Screen.get_table_data(start_row, end_row),get_table_data_by_positions(...)andget_mapped_table_data(start_row, end_row, cls, mapper, column_positions=None). - The Save Screen buffer format changed, so a buffer saved by an older version can't be restored.
Contributing
-
Clone the repo and install dependencies with
uv:uv sync --locked
-
Make your changes under
src/ibm5250/. -
Lint, type-check and run the test suite before opening a PR:
uv run ruff check . uv run mypy uv run pytest
-
Branch off
developand open your pull request againstdevelop(notmain— see Branching & releases below).
There's no separate contribution guide configured in this repo — just make
sure ruff check, mypy and pytest all pass.
Branching & releases
Every push runs the test suite in CI. What gets built and published to PyPI depends on the branch:
| Branch | Tested | Built | Published to PyPI | Version format |
|---|---|---|---|---|
main |
✅ | ✅ | ✅ production | X.Y.Z (patch auto-incremented from the latest release) |
develop |
✅ | ✅ | ✅ development | X.Y.Z.devN |
| any other branch | ✅ | ✅ | ❌ (build artifact only) | n/a |
- Contribute by branching off
developand opening PRs intodevelop.developpublishes.devNreleases so changes can be tried out before they're promoted tomain. mainis production — merges there are published as real, patch-bumped releases on PyPI.- The version in
pyproject.tomlis a base version only and is never hand-bumped for a release;scripts/compute_version.pycomputes the actual published version from what's already on PyPI at build time. See.github/workflows/README.mdfor the full CI/publishing setup.
License
Licensed under the Mozilla Public License 2.0.
Metadata
Release files for ibm5250 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ibm5250-0.1.6.tar.gz | 86.7 kB | With provenance | Details for ibm5250-0.1.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ibm5250-0.1.6-py3-none-any.whl | Python 3 | none | any | Details for ibm5250-0.1.6-py3-none-any.whl |
Total release size: 146.9 kB
Release files / ibm5250-0.1.6.tar.gz
| Download URL | ibm5250-0.1.6.tar.gz |
|---|---|
| Size | 86.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
36bb15a766c1baf47ec0cb1c582c5be42b2b766d3b99fdd8ade3e392aaace4b1
|
|
BLAKE2b-256 checksum How to use checksums |
ff35ecad36054dda1958c4c47f4f782abd36c6bb0671d5bb3196dcaec17bd22b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.
Transparency logRelease files / ibm5250-0.1.6-py3-none-any.whl
| Download URL | ibm5250-0.1.6-py3-none-any.whl |
|---|---|
| Size | 60.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0fb0ef5ea6978e869f66aa61fe24f2fffdcb5eb4218536f2267e469706e7ca57
|
|
BLAKE2b-256 checksum How to use checksums |
88dfbe2573d4a5d222fd6a1bf2fd3873f5a08b95f4b45390ba29ddee875e5be0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.
Transparency logSecurity for ibm5250 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for ibm5250-0.1.6.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.