473,788 Members | 2,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Telne t(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 6460
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.Telne t(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******* *************@n ews20.bellgloba l.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.Telne t(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******@chipw orks.net> wrote in message
news:ab******** *************** ***@posting.goo gle.com...
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
4983
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 gone back to basics on the code for simplicity here; Linux 2.6 : Python 2.3.4
9
11347
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) using System.Net.Sockets;
2
10774
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 the client machine to close per my network app the computer fills up w/ thousands of these :0 TCP foobox:8888 localhost:2188 TIME_WAIT :0 TCP foobox:8888 localhost:2189 TIME_WAIT :0 TCP foobox:8888 localhost:2190 TIME_WAIT
8
41436
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 into a CLOSE_WAIT state ad infinitum and never closes fully until my client and server applications shut down. Ive disabled lingeroptions. Is there anything else easy that i can do to force the loopback to close immediately?
4
11410
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 the socket and if i got error then the socket is closed. there must be a way to get this event without trying to send something , since it is in the lower level
1
8388
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 seen) that would allow me to log into a Cisco router, enter configuration mode, change an interface description and also grab a snapshot of the running configuration. So far I've managed to be able to log in and change the interface configuration....
2
3895
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
1475
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 logger_server.debug(self.__class__.__name__ + ' set data = True')
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10374
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10121
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4076
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2898
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.