Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32006

Re: The pty module, reading from a pty, and Python 2/3

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <dwightdhutto@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'except:': 0.07; 'try:': 0.07; '"w")': 0.09; 'filename)': 0.09; 'oserror': 0.09; 'received:mail-qc0-f174.google.com': 0.09; 'spawn': 0.09; 'subject:module': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'size,': 0.13; 'pty': 0.16; 'sys.exit(1)': 0.16; 'integer': 0.17; 'input': 0.18; 'skip:p 30': 0.20; 'import': 0.21; 'cc:2**0': 0.23; 'random': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'in.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:/': 0.28; 'read,': 0.29; 'file': 0.32; 'received:google.com': 0.34; 'received:209.85': 0.35; 'except': 0.36; 'characters': 0.36; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'where': 0.40; 'header:Received:5': 0.40; 'below,': 0.60; 'subject:, ': 0.61; 'within': 0.64; 'subject:The': 0.71
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=h8n6eEaUA43NFT5cC1ihg2EYDO4LqA66MxwKMugwnEg=; b=UvsY0ZTKtwNZuJ17IoYEjVRkajgqqsl7uSLb7LpbX6wD6aLNZfU4uYDxzX6PwuCsEn qpVyUgMnd6oYxb+VQulqhvUN580lenlDrbpA55UirDxc69ol3VcU1QKTOF8tRjjM+nYl lDb4Jp/NGCDUH+ek03FYUuoB1aSpR3nrW3gx2HKXvZS1j7Mikud+4U9KO6i2pkFkFXk5 Ou4UiK8VDK8qm8BGkD6uGfrTaXc2/ZmSwjdS2PDROquFNJSP/R8qJcjmjnasLPma9uZD 0Ozd/nPjZu3TcWhQu6SQf8Jxk738ZMupxxxmq3q4GeT0amjm3wAa+fBcEXesXRTOeVK5 dHgg==
MIME-Version 1.0
In-Reply-To <CA+vVgJWV3hNxy8pM6JC4hc=yPtOY4XLUQFww7pE3=cYajaUJUQ@mail.gmail.com>
References <508759A9.7070601@wisc.edu> <50875A83.6030806@wisc.edu> <50876356.3030005@wisc.edu> <CA+vVgJWV3hNxy8pM6JC4hc=yPtOY4XLUQFww7pE3=cYajaUJUQ@mail.gmail.com>
Date Wed, 24 Oct 2012 02:33:45 -0400
Subject Re: The pty module, reading from a pty, and Python 2/3
From David Hutto <dwightdhutto@gmail.com>
To Evan Driscoll <edriscoll@wisc.edu>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2729.1351060428.27098.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351060428 news.xs4all.nl 6896 [2001:888:2000:d::a6]:38913
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32006

Show key headers only | View raw


import sys
import pty
import os

def get_text(filename):
#you need to find the file size, and place it as an integer in read
below, where you return the value
	statinfo = os.stat(filename)

	try:
		( child_pid, fd ) = pty.fork()		# OK
	except OSError as e:
		print(str(e))
		sys.exit(1)

	if child_pid == 0:
		try:
			with open("log.txt", "w") as f:
				f.write("about to execlp")
			os.execlp("cat", "cat", filename)
		except:
			with open("log.txt", "w") as f:
				f.write("could not spawn process")
			print("Could not spawn")
			sys.exit(1)

	child_pty = os.fdopen(fd)
#you have to input into read, how many characters you want read in. if
you place a random integer in, it will read to that integer within the
file
	return child_pty.read(statinfo.st_size)


if __name__ == "__main__":
	print(get_text("my-pty-test.py"))


-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: The pty module, reading from a pty, and Python 2/3 David Hutto <dwightdhutto@gmail.com> - 2012-10-24 02:33 -0400

csiph-web