473,322 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

telnetlib close not closing sockets

I'm rather new to python but a long time programmer. I think I've
covered my bases so I hope this isn't somthing obvious.

I'm running ActiveState PythonWin 2.3.2 (#49, Nov 13 2003, 10:34:54)
[MSC v.1200 32 bit (Intel)] on win32.

I have several processes that are polling a telnet server on a regular
basis. I'm using telnetlib for this. When I issue the .close method
the connection is not closed (no TCP FIN is sent to the server).

import getpass
import sys
import telnetlib
import smtplib
import time
import os

HOST = "fred"
tn = telnetlib.Telnet(HOST)
tn.write("DEMO\n")
print tn.read_until("\n>",5)
tn.write("OFF\n")
print tn.read_until("Logon Please:",5)
print tn.read_until("make sure socket queue is empty the hard way",5)
tn.close

I've tried several options to accomplish this, but for whatever reason
the socket remains open until I exit python. Since I'm polling in a
loop I'd prefer not to exit python. I must close the tcp connection
after each pass or I'll consume server licenses.

Any of you pythoneers have a clue where I should be looking? Is this
a bug in telnetlib, the socket handlers, or the windows port? Am I
missing something obvious? Telnetlib looks like it is making the
right close calls.

Thanks,

Mike
A python newb
Jul 18 '05 #1
3 6413
Hello Mike,

Your subject remembered me some hard time using telnetlib on Windows. Oh God
I searched for answers!:o) I would be very happy to prevent you this. To
verify if you have the same problem, Could you tell me if you always reach
the timeout (5 secs) before your read_until() return?

For your current problem. Did you try with parenthesis after "close":
tn.close()

I hope it was just that!

Yannick

Mike Monaghan wrote:
HOST = "fred"
tn = telnetlib.Telnet(HOST)
tn.write("DEMO\n")
print tn.read_until("\n>",5)
tn.write("OFF\n")
print tn.read_until("Logon Please:",5)
print tn.read_until("make sure socket queue is empty the hard way",5)
tn.close

I've tried several options to accomplish this, but for whatever reason
the socket remains open until I exit python.

Jul 18 '05 #2
Yannick,

The parenthesis after the tn.close() did the trick. I know I've made
that mistake somewhere else. Telnetlib's documentation even clearly
says .close() and I bet they mean the () is required. Thank you!!!

As for read_until, I'm not reaching timeout there. I am however
setting a timeout in the statement like so:

print tn.read_until("\n>",5)

Maybe that change is making it work for me. Did you set a timeout
value when you were testing?

I do have a problem with an expect, but I'm 99% sure a control
character in the stream is missing from my expect statement because
some of the strings are being detected. I'm going to play with it
again tonight.

I have to say I'm much impressed with the python language,
documentation, and community. It will take a while to understand some
of the doc as they are written in a very technical manner. I
personally think its great because most software efforts are plagued
by poor documentation. So far all my problems have been my own.

Thanks again for your help!

Mike

Yannick Turgeon <no****@nowhere.com> wrote in message news:<nw********************@news20.bellglobal.com >...
Hello Mike,

Your subject remembered me some hard time using telnetlib on Windows. Oh God
I searched for answers!:o) I would be very happy to prevent you this. To
verify if you have the same problem, Could you tell me if you always reach
the timeout (5 secs) before your read_until() return?

For your current problem. Did you try with parenthesis after "close":
tn.close()

I hope it was just that!

Yannick

Mike Monaghan wrote:
HOST = "fred"
tn = telnetlib.Telnet(HOST)
tn.write("DEMO\n")
print tn.read_until("\n>",5)
tn.write("OFF\n")
print tn.read_until("Logon Please:",5)
print tn.read_until("make sure socket queue is empty the hard way",5)
tn.close

I've tried several options to accomplish this, but for whatever reason
the socket remains open until I exit python.

Jul 18 '05 #3
Mike,

Concerning you "expect()" problem (which is very similar to read_until())
Probably that it's working for 1, 2, 3 commands... but after a couple more
it's no more working? Is that your situation? I'll tell you a terrible
secret: Window's telnet is not sending you back only your command result.

You should read this post before working any harder:
http://groups.google.ca/groups?selm=...40news20.bellg
lobal.com

The solution:

If you're facing this, you'll have to use a Telnet Program using
"stream-mode". I'm not sure Windows' Telnet can do this. After long hours
searching what was happening, to finally find it was caused by Windows'
Telnet not respecting Telnet Protocol, my company bougth "Pragma
TelnetServer" and all my telnetlib problem finally went away. I learned
afterward about "stream-mode" so I don't know if Windows' Telnet offer this.

Really hope this help.

Yannick
"Mike Monaghan" <ju******@chipworks.net> wrote in message
news:ab**************************@posting.google.c om...
I do have a problem with an expect, but I'm 99% sure a control
character in the stream is missing from my expect statement because
some of the strings are being detected. I'm going to play with it
again tonight.

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Svha | last post by:
Greetings I'd like to do some tweaks on a telnet session as I have no control over the server side what so ever. However I cannot correctly negotiate a telnet connection to the server. I've...
9
by: AA | last post by:
This is making me crazy!! Please, if some body can help me. I'm testing a ver simple socket client. In my test I just open and close a connection (in a loop) to my local IIS server (port 80)...
2
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
8
by: Claire | last post by:
I'm trying to debug my network application ie I want to check my error handling when the connection is broken. Im using 127.0.0.1 as the connection address. Unfortunately, the client socket goes...
4
by: Haim | last post by:
it is very strange for me that a simple event of closing socket that was in the the winsock object of vb6 , i didn't found yet in the vb.net the only way i found is to try to send something to...
1
by: dudds | last post by:
Hi Guys, I just started learning Python a couple of days ago and to put some of what I learnt into practice. As such I thought I might try and write a simple program (based on examples I had...
2
by: Samuel | last post by:
Hi, When using telnetlib, the connection sometimes breaks with the following error: "error: (32, 'Broken pipe')" where the traceback points to self.sock.send(buffer)
3
by: Daniel | last post by:
Hello, I can't seem to get my sockets code to work right. Here is what I have inside my RequestHandler handle() function: total_data= data = True...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.