pure3270.session Module

Session management for pure3270, handling synchronous and asynchronous 3270 connections.

exception pure3270.session.SessionError(message: str, context: Dict[str, Any] | None = None)[source]

Bases: Exception

Base exception for session-related errors.

When context is provided, include key details in the string representation so tests can assert on them without reaching into attributes.

__init__(message: str, context: Dict[str, Any] | None = None)[source]
exception pure3270.session.ConnectionError(message: str, context: Dict[str, Any] | None = None)[source]

Bases: SessionError

Raised when connection fails.

class pure3270.session.Session(host: str | None = None, port: int = 23, ssl_context: Any | None = None, force_mode: str | None = None, allow_fallback: bool = True, enable_trace: bool = False, terminal_type: str = 'IBM-3278-2')[source]

Bases: object

Synchronous wrapper for AsyncSession.

This class provides a synchronous interface to the asynchronous 3270 session. All methods use asyncio.run() to execute async operations.

__init__(host: str | None = None, port: int = 23, ssl_context: Any | None = None, force_mode: str | None = None, allow_fallback: bool = True, enable_trace: bool = False, terminal_type: str = 'IBM-3278-2') None[source]

Initialize a synchronous session with a dedicated thread and event loop.

connect(host: str | None = None, port: int | None = None, ssl_context: Any | None = None) None[source]

Connect to the 3270 host synchronously.

Parameters:
  • host – Optional host override.

  • port – Optional port override (default 23).

  • ssl_context – Optional SSL context override.

Raises:

ConnectionError – If connection fails.

send(data: bytes) None[source]

Send data to the session.

Parameters:

data – Bytes to send.

Raises:

SessionError – If send fails.

read(timeout: float = 5.0) bytes[source]

Read data from the session.

Parameters:

timeout – Read timeout in seconds.

Returns:

Received bytes.

Raises:

SessionError – If read fails.

get_aid() int | None[source]

Get AID synchronously (last known AID value).

close() None[source]

Close the session synchronously.

property connected: bool

Check if session is connected.

get_trace_events() List[Any][source]
open(host: str, port: int = 23) None[source]

Open connection synchronously (s3270 Open() action).

property screen_buffer: ScreenBuffer

Expose screen buffer for sync Session tests.

property input_inhibited: bool

Get Input Inhibited state (keyboard locked).

property tn3270e_mode: bool

Check if session is in TN3270E mode.

Preference order: 1) Async session’s tn3270 mode flag when True 2) Handler’s negotiated_tn3270e flag as a fallback

close_script() None[source]

Close script synchronously (s3270 CloseScript() action).

ascii(data: bytes) str[source]

Convert EBCDIC data to ASCII text (s3270 Ascii() action).

Parameters:

data – EBCDIC bytes to convert.

Returns:

ASCII string representation.

ebcdic(text: str) bytes[source]

Convert ASCII text to EBCDIC data (s3270 Ebcdic() action).

Parameters:

text – ASCII text to convert.

Returns:

EBCDIC bytes representation.

ascii1(byte_val: int) str[source]

Convert a single EBCDIC byte to ASCII character.

This is a pure conversion utility and does not require a live session.

ebcdic1(char: str) int[source]

Convert a single ASCII character to EBCDIC byte.

This is a pure conversion utility and does not require a live session.

ascii_field(field_index: int) str[source]

Convert field content to ASCII text (s3270 AsciiField() action).

Parameters:

field_index – Index of field to convert.

Returns:

ASCII string representation of field content.

cursor_select() None[source]

Select field at cursor (s3270 CursorSelect() action).

Raises:

SessionError – If not connected.

delete_field() None[source]

Delete field at cursor (s3270 DeleteField() action).

Raises:

SessionError – If not connected.

string(text: str) None[source]

Send string to the session (s3270 String() action).

circum_not() None[source]

Toggle circumvention of field protection (s3270 CircumNot() action).

Raises:

SessionError – If not connected.

script(commands: str) None[source]

Execute script (s3270 Script() action).

Parameters:

commands – Script commands.

Raises:

SessionError – If not connected.

execute(command: str) str[source]

Execute external command synchronously (s3270 Execute() action).

info() str[source]

Get session information synchronously.

query(query_type: str = 'All') str[source]

Query session information synchronously.

Supported query types (s3270 protocol parity):
  • All: Summary information

  • CodePage: Host code page

  • ConnectionState: Connection state

  • Cursor: Cursor position

  • Model: 3270 model information

  • ScreenCurSize: Current screen dimensions

  • Tn3270eOptions: Active TN3270E functions

  • TelnetHostOptions: Host TELNET options

  • TelnetMyOptions: Client TELNET options

  • And 30+ more query types…

move_cursor1(row: int, col: int) None[source]

Move cursor to specified position (s3270 MoveCursor1 action).

hex_string(hex_data: str) None[source]

Send raw hex bytes as input (s3270 HexString() action).

set(option: str, value: str) None[source]

Set option synchronously.

print_text(text: str) None[source]

Print text synchronously.

snap() None[source]

Take snapshot synchronously.

show() None[source]

Show screen synchronously.

trace(on: bool = True) None[source]

Enable/disable tracing synchronously.

wait(seconds: float = 1.0) None[source]

Wait synchronously.

sleep(seconds: float = 1.0) None[source]

Sleep synchronously.

transfer(file: str) None[source]

Transfer file synchronously.

source(file: str) None[source]

Source file synchronously.

expect(pattern: str, timeout: float = 10.0) bool[source]

Expect pattern synchronously.

fail(message: str) None[source]

Fail with message synchronously.

compose(text: str) None[source]

Compose text synchronously.

set_field_attribute(field_index: int, attr: str, value: int) None[source]

Set a screen field attribute via the underlying async session.

This is a synchronous passthrough used by compatibility tests; the underlying implementation is synchronous as well.

cookie(cookie_string: str) None[source]

Set cookie synchronously.

interrupt() None[source]

Send interrupt synchronously (s3270 Interrupt() action).

key(keyname: str) None[source]

Send key synchronously (s3270 Key() action).

submit(aid: int) None[source]

Submit with AID synchronously.

home() None[source]

Move cursor to home position.

up() None[source]

Move cursor up.

down() None[source]

Move cursor down.

tab() None[source]

Move cursor to next tab stop.

backtab() None[source]

Move cursor to previous tab stop.

backspace() None[source]

Send backspace key.

enter() None[source]

Send Enter key.

pf(n: str) None[source]

Send PF key.

pa(n: str) None[source]

Send PA key.

erase() None[source]

Erase entire screen.

clear() None[source]

Clear entire screen (alias for erase).

newline() None[source]

Move cursor to start of next line.

erase_eof() None[source]

Erase to end of field.

erase_input() None[source]

Erase all input fields.

field_end() None[source]

Move cursor to end of field.

field_mark() None[source]

Set field mark.

dup() None[source]

Duplicate character.

pause(seconds: float = 1.0) None[source]

Pause session.

bell() None[source]

Ring bell.

left2() None[source]

Move cursor left by 2.

right() None[source]

Move cursor right.

right2() None[source]

Move cursor right by 2.

reset() None[source]

Reset session.

field_exit() None[source]

Exit field.

sysreq() None[source]

Send SysReq key.

attn() None[source]

Send Attention key.

test() None[source]

Send Test key.

left() None[source]

Move cursor left.

class pure3270.session.AsyncSession(host: str | None = None, port: int = 23, ssl_context: Any | None = None, force_mode: str | None = None, allow_fallback: bool = True, enable_trace: bool = False, terminal_type: str = 'IBM-3278-2', is_printer_session: bool = False)[source]

Bases: object

Asynchronous 3270 session handler.

This class provides an asynchronous interface to the 3270 session. All operations are async and use asyncio for non-blocking I/O.

AID_MAP: Dict[str, int] = {'Attn': 241, 'BackSpace': 248, 'CLEAR': 109, 'Dup': 245, 'Enter': 125, 'PA(1)': 108, 'PA(2)': 110, 'PA(3)': 107, 'PF(1)': 241, 'PF(10)': 122, 'PF(11)': 123, 'PF(12)': 124, 'PF(13)': 193, 'PF(14)': 194, 'PF(15)': 195, 'PF(16)': 196, 'PF(17)': 197, 'PF(18)': 198, 'PF(19)': 199, 'PF(2)': 242, 'PF(20)': 200, 'PF(21)': 201, 'PF(22)': 74, 'PF(23)': 75, 'PF(24)': 76, 'PF(3)': 243, 'PF(4)': 244, 'PF(5)': 245, 'PF(6)': 246, 'PF(7)': 247, 'PF(8)': 248, 'PF(9)': 249, 'RESET': 106, 'SysReq': 240, 'TEST': 17, 'Test': 17}
__init__(host: str | None = None, port: int = 23, ssl_context: Any | None = None, force_mode: str | None = None, allow_fallback: bool = True, enable_trace: bool = False, terminal_type: str = 'IBM-3278-2', is_printer_session: bool = False) None[source]

Initialize the AsyncSession.

Parameters:
  • host – Host to connect to.

  • port – Port to connect to.

  • ssl_context – SSL context for secure connections.

  • force_mode – Force specific TN3270 mode.

  • allow_fallback – Allow fallback to TN3270 if TN3270E fails.

  • enable_trace – Enable tracing.

  • terminal_type – Terminal model type.

  • is_printer_session – True if this is a printer session.

resources: Dict[str, str]
property host: str | None

Public host accessor for tests.

property port: int

Public port accessor for tests.

property model: str
property screen_buffer: ScreenBuffer

Get the screen buffer.

property screen: ScreenBuffer

Get the screen buffer (alias for screen_buffer).

property input_inhibited: bool

Get Input Inhibited state (keyboard locked).

property handler: TN3270Handler | None

Get the underlying TN3270 handler (may be None if not connected).

property tn3270e_mode: bool

Check if session is in TN3270E mode.

property connected: bool

Check if session is connected.

async connect(host: str | None = None, port: int | None = None, ssl_context: Any | None = None) None[source]

Connect to the 3270 host asynchronously.

async send(data: bytes) None[source]

Send data to the session with retry logic.

async send_data(data: bytes) None[source]

Send data to the session (alias for send).

async read(timeout: float = 5.0) bytes[source]

Read data from the session with retry logic.

async receive_data(timeout: float = 5.0) bytes[source]

Receive data from the session (alias for read).

get_aid() int | None[source]

Get the last AID value.

async close() None[source]

Close the session.

async close_script() None[source]

Close script (s3270 CloseScript() action).

get_trace_events() List[Any][source]

Get trace events.

managed() AsyncIterator[AsyncSession][source]

Provide a managed async context that guarantees close() on exit.

ascii(data: bytes) str[source]

Convert EBCDIC to ASCII.

ebcdic(text: str) bytes[source]

Convert ASCII to EBCDIC.

ascii1(byte_val: int) str[source]

Convert single EBCDIC byte to ASCII.

ebcdic1(char: str) int[source]

Convert single ASCII char to EBCDIC.

ascii_field(field_index: int) str[source]

Convert field to ASCII.

async cursor_select() None[source]

Select field at cursor.

async delete_field() None[source]

Delete field at cursor.

async insert_text(text: str) None[source]

Insert text at cursor.

async string(text: str) None[source]

Send string to the session.

async circum_not() None[source]

Toggle circumvention.

async script(commands: str) None[source]

Execute script command(s).

Supports both simple method names and function-style commands like: - Simple: “connect”, “disconnect” - With args: “String(test)”, “MoveCursor(5,10)”

async execute(command: str) str[source]

Execute external command.

async interrupt() None[source]

Send interrupt.

async key(keyname: str) None[source]

Send key synchronously (s3270 Key() action).

capabilities() str[source]

Get capabilities.

async set_option(option: str, value: str) None[source]

Set option (placeholder for future functionality).

async query(query_type: str = 'All') str[source]

Query screen.

async set(option: str, value: str) None[source]

Set option (alias).

async exit() None[source]

Exit session.

async bell() None[source]

Ring bell.

async pause(seconds: float = 1.0) None[source]

Pause.

async ansi_text(data: bytes) str[source]

Convert EBCDIC to ANSI.

async hex_string(hex_str: str) bytes[source]

Convert hex to bytes.

async show() None[source]

Show screen.

async snap() None[source]

Save snapshot.

async newline() None[source]

Move cursor to start of next line.

async page_down() None[source]

Page down: wrap to top row for tests.

async page_up() None[source]

Page up: move to top row for tests.

async paste_string(text: str) None[source]
async end() None[source]

Move cursor to end of current line.

async move_cursor(row: int, col: int) None[source]
async move_cursor1(row1: int, col1: int) None[source]
async next_word() None[source]
async previous_word() None[source]
async toggle_insert() None[source]
async flip() None[source]
async insert() None[source]
async delete() None[source]

Delete character at cursor by shifting remainder left and clearing last.

async disconnect() None[source]
async left() None[source]

Move cursor left.

async right() None[source]

Move cursor right.

async left2() None[source]

Move cursor left by 2.

async right2() None[source]

Move cursor right by 2.

async mono_case() None[source]

Toggle monocase.

async nvt_text(text: str) None[source]

Send NVT text.

async print_text(text: str) None[source]

Print text.

async prompt(message: str) str[source]

Prompt for input.

async read_buffer() bytes[source]

Read buffer.

async reconnect() None[source]

Reconnect.

async info() str[source]

Get session information.

async quit() None[source]

Quit the session.

async home() None[source]

Move cursor to home position.

async up() None[source]

Move cursor up.

async down() None[source]

Move cursor down.

async tab() None[source]

Send Tab key.

async backtab() None[source]

Move cursor to previous tab stop.

async backspace() None[source]

Send backspace key.

async enter() None[source]

Send Enter key.

async clear() None[source]

Clear screen locally.

async pf(n: str) None[source]

Send PF key.

async pa(n: str) None[source]

Send PA key.

async macro(commands: List[str]) None[source]

Execute a sequence of commands.

async erase() None[source]

Erase entire screen (local).

async erase_eof() None[source]

Erase to end of field.

async erase_input() None[source]

Erase all input fields.

async field_end() None[source]

Move cursor to end of field.

async field_mark() None[source]

Set field mark.

async dup() None[source]

Duplicate character.

async field_exit() None[source]

Exit field.

async sysreq() None[source]

Send SysReq key.

async attn() None[source]

Send Attention key.

async test() None[source]

Send Test key.

async screen_trace() None[source]

Screen trace.

async source(file: str) None[source]

Source script.

async subject_names() None[source]

Subject names.

async sys_req(command: str | None = None) None[source]

Send SysReq.

async send_break() None[source]

Send Telnet BREAK.

async send_soh_message(status_code: int) None[source]

Send SOH message.

async toggle_option(option: str) None[source]

Toggle option.

async trace(on: bool) None[source]

Enable/disable tracing.

async transfer(file: str) None[source]

Transfer file.

async send_file(local_path: str, remote_name: str) None[source]

Send a file to the host using IND$FILE protocol.

async receive_file(remote_name: str, local_path: str) None[source]

Receive a file from the host using IND$FILE protocol.

async wait_condition(condition: str) None[source]

Wait for condition.

async compose(text: str) None[source]

Compose text.

async cookie(cookie_string: str) None[source]

Set a cookie.

This sets the cookie immediately and returns None. Tests can call this without awaiting since the side-effect happens immediately.

async expect(pattern: str, timeout: float = 10.0) bool[source]

Wait for pattern.

async fail(message: str) None[source]

Fail with message.

async select_light_pen(row: int, col: int) None[source]

Select light pen.

async start_lu_lu_session(lu_name: str) None[source]

Start LU-LU session.

property lu_lu_session: Any

Get the current LU-LU session if active.

async load_resource_definitions(file_path: str) None[source]

Load resources.

async apply_resources() None[source]

Apply resources.

set_field_attribute(field_index: int, attr: str, value: int) None[source]

Set field attribute.

async submit(aid: int) None[source]

Submit with AID.