Skip to main content

Intro

  1. Automatic interaction with a console program
  2. Automatic interaction with a socket

Install

pip install qinteract

Example

ProcessInteraction

Program to be called (want_right.py):

# coding:utf8
# python3

import random

first_num = str(random.randint(0, 10))
second_num = str(random.randint(0, 10))

first_input = input('input {}: '.format(first_num))
second_input = input('input {}: '.format(second_num))

if first_num==first_input and second_num==second_input:
    print('Right!')
else:
    print('Wrong!')

Interaction:

# coding:utf8
# python3

from qinteract import ProcessInteraction

command = ['python', 'want_right.py']

pi = ProcessInteraction(command)
for i in range(2):
    content = pi.recvuntil_re(r'input (\d+): ')
    print(content[0])
    num = content[1].group(1)
    print(repr(num))
    pi.sendline(num)
content = pi.recvline()
print(content)

SocketInteraction

Server side program (server.py):

# coding:utf8
# python3

import random
import socket
import sys

# Create socket object
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

port = 9999

# Bind the port
serversocket.bind(('', port))

# Set the maximum number of connections to queue after exceeding
serversocket.listen(5)

while True:
    try:
        print('Wait for connection...')
        # Establish client connection
        clientsocket, addr = serversocket.accept()
        print('Connection address: ', addr)

        first_num = str(random.randint(0, 10))
        second_num = str(random.randint(0, 10))

        clientsocket.send('input {}: '.format(first_num).encode('utf8'))
        first_input = clientsocket.recv(10).decode('utf8').strip()
        clientsocket.send('input {}: '.format(second_num).encode('utf8'))
        second_input = clientsocket.recv(10).decode('utf8').strip()

        print(first_input)
        print(second_input)

        if first_num == first_input and second_num == second_input:
            clientsocket.send('Right!\n'.encode('utf8'))
        else:
            clientsocket.send('Wrong!\n'.encode('utf8'))
        clientsocket.shutdown(socket.SHUT_RDWR)
        clientsocket.close()
    except KeyboardInterrupt as identifier:
        print('The program was stopped manually')
        break

serversocket.close()

Interaction:

# coding:utf8
# python3

from qinteract import SocketInteraction

si = SocketInteraction('127.0.0.1', 9999)
for i in range(2):
    content = si.recvuntil_re(r'input (\d+): ')
    print(content[0])
    num = content[1].group(1)
    print(repr(num))
    si.sendline(num)
content = si.recvline()
print(content)

Metadata

Release files for qinteract 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for qinteract 0.1.1
File Size Uploaded
qinteract-0.1.1.tar.gz 3.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for qinteract 0.1.1
File Interpreter ABI Platform
qinteract-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.5 kB

Release files / qinteract-0.1.1.tar.gz

Download URL qinteract-0.1.1.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2ac053f407e061ebb6e5070df34ed12282a3c9071e27ab0151d310ee642da66a
BLAKE2b-256 checksum
How to use checksums
128d786b9ac8707e43c8e6d90d7c52814338b459d9932c27816235fef6f5c5ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

Release files / qinteract-0.1.1-py3-none-any.whl

Download URL qinteract-0.1.1-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e10eb30b45b070f6a7728d98b46528aa41c6616455fd69ff89a0d191b7994f7
BLAKE2b-256 checksum
How to use checksums
efa96ee5744f2d09c19eec561d01f12fe4eeb033dc7dd5e2d32bbf4d6ba66ab1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page