By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

Terminating a subprocess question

P: n/a
I'm opening a telnet session with the subprocess call below. I then
wait ten seconds and attempt to terminate the telnet window I created.
Unfortuantely, the telnet window remains after the 'TerminateProcess"
call below. This software works great for opening an executable
directly (i.e. Handle = subprocess.Popen("myProgram.exe), but here I'm
using the Windows 'start' command inside, which I think has an effect.
Is there another way I can force termination of the telnet session I
create ?

# CODE STARTS HERE

TSS_Log_Path = "C:\\Log_Outputs\\TSS_Log_Test.txt"
TSS_Handle = subprocess.Popen("start telnet.exe -f " + TSS_Log_Path + "
localhost 6000",shell=True)
time.sleep(10)
ctypes.windll.kernel32.TerminateProcess(int(TSS_Ha ndle._handle), -1) #
Terminate the TSS_Log

# END CODE

Mar 29 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Ernesto wrote:
I'm opening a telnet session with the subprocess call below. I then
wait ten seconds and attempt to terminate the telnet window I created.
Unfortuantely, the telnet window remains after the 'TerminateProcess"
call below. This software works great for opening an executable
directly (i.e. Handle = subprocess.Popen("myProgram.exe), but here I'm
using the Windows 'start' command inside, which I think has an effect.
Is there another way I can force termination of the telnet session I
create ?


Maybe using the module telnetlib instead?

Diez
Mar 29 '06 #2

P: n/a

Diez B. Roggisch wrote:
Ernesto wrote:
I'm opening a telnet session with the subprocess call below. I then
wait ten seconds and attempt to terminate the telnet window I created.
Unfortuantely, the telnet window remains after the 'TerminateProcess"
call below. This software works great for opening an executable
directly (i.e. Handle = subprocess.Popen("myProgram.exe), but here I'm
using the Windows 'start' command inside, which I think has an effect.
Is there another way I can force termination of the telnet session I
create ?


Maybe using the module telnetlib instead?

Diez


oh ok great ! But how would I create a log file. Couldn't seem to
find that here:

http://python.active-venture.com/lib...t-objects.html

http://pydoc.org/1.6/telnetlib.html

Plus, everytime, I tried to use the read_all object, my program crashed
pretty hard...

tn = telnetlib.Telnet("localhost",6000)
print tn.read_all()
# CRASH

Mar 29 '06 #3

P: n/a
Ernesto wrote:
Plus, everytime, I tried to use the read_all object, my program crashed
pretty hard...

tn = telnetlib.Telnet("localhost",6000)
print tn.read_all()
# CRASH


that's an unusual error message. are you sure you didn't get a
traceback? if so, what did it say?

</F>

Mar 29 '06 #4

P: n/a

Fredrik Lundh wrote:
Ernesto wrote:
Plus, everytime, I tried to use the read_all object, my program crashed
pretty hard...

tn = telnetlib.Telnet("localhost",6000)
print tn.read_all()
# CRASH


that's an unusual error message. are you sure you didn't get a
traceback? if so, what did it say?

</F>


I was running it directly in a python shell. After the tn.read_all()
call, the python shell window freezes up, and I have to do a hard
termination of the shell. There is no traceback message, just a freeze.

Mar 29 '06 #5

P: n/a
Ernesto wrote:
tn = telnetlib.Telnet("localhost",6000)
print tn.read_all()
# CRASH


that's an unusual error message. are you sure you didn't get a
traceback? if so, what did it say?


I was running it directly in a python shell. After the tn.read_all()
call, the python shell window freezes up, and I have to do a hard
termination of the shell. There is no traceback message, just a freeze.


hmm. that hardly qualifies as a "CRASH"; rather, it's pretty much
what you'd expect from a read_all call:
help(tn.read_all) Help on method read_all in module telnetlib:

read_all(self) method of telnetlib.Telnet instance
Read all data until EOF; block until connection closed.

(note the "block until connection closed" part)

what happens if you use a non-blocking method instead ? e.g.
help(tn.read_very_eager)

Help on method read_very_eager in module telnetlib:

read_very_eager(self) method of telnetlib.Telnet instance
Read everything that's possible without blocking in I/O (eager).

Raise EOFError if connection closed and no cooked data
available. Return '' if no cooked data available otherwise.
Don't block unless in the midst of an IAC sequence.

</F>

Mar 29 '06 #6

P: n/a

Ernesto wrote:
I'm opening a telnet session with the subprocess call below. I then
wait ten seconds and attempt to terminate the telnet window I created.
Unfortuantely, the telnet window remains after the 'TerminateProcess"
call below. This software works great for opening an executable
directly (i.e. Handle = subprocess.Popen("myProgram.exe), but here I'm
using the Windows 'start' command inside, which I think has an effect.
Is there another way I can force termination of the telnet session I
create ?

# CODE STARTS HERE

TSS_Log_Path = "C:\\Log_Outputs\\TSS_Log_Test.txt"
TSS_Handle = subprocess.Popen("start telnet.exe -f " + TSS_Log_Path + "
localhost 6000",shell=True)
time.sleep(10)
ctypes.windll.kernel32.TerminateProcess(int(TSS_Ha ndle._handle), -1) #
Terminate the TSS_Log

# END CODE


Actually, the original answer I was looking for to for a "hard close"
of telnet in Windows is:

TSS_Handle = subprocess.Popen("TASKKILL /F /IM telnet.exe", shell=True)

It is almost definitely true though that it is safer to use telnetlib.
I just don't have the time to adjust my entire huge application at the
moment. Thanks all.

Mar 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.