Terminating a subprocess question

E

Ernesto

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_Handle._handle), -1) #
Terminate the TSS_Log

# END CODE
 
D

Diez B. Roggisch

Ernesto said:
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
 
F

Fredrik Lundh

Ernesto said:
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>
 
E

Ernesto

Fredrik said:
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.
 
F

Fredrik Lundh

Ernesto said:
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 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 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>
 
E

Ernesto

Ernesto said:
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_Handle._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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top