[Tutor] python telnet
Rayon
evosweet at hotmail.com
Sun Oct 23 02:13:04 CEST 2011
Well what I am doing is connecting to a telnet session sending some
commands, exiting and returning some data.
What I would to do is send the commands and return the data without exiting
the session.
I would like to keep the same session and just send commands and return
data.
#!/usr/bin/env python
import telnetlib
class hlr_com():
#get host and command
def __init__(self):
"""init host and command """
self.user_name = '********'
self.password = '**********'
self.host = '172.20.50.176'
self.command = ''
#edit top of return
def fix_return(self,hia_return):
"""
edit the top of data returned form the hia
"""
try:
data = str(hia_return).strip('\r')
return data
except Exception,error:
logs("error",str(err),'null')
#set host ip address
def set_host(self,host):
""" set host ipaddress"""
self.host = host
#send command to hia and end session
def hlr_telnet(self,command):
"""
connect to host and execute command and
exit
"""
try:
hlr_tel = telnetlib.Telnet(self.host)
hlr_tel.read_until('login:')
hlr_tel.write(self.user_name+"\r")
hlr_tel.read_until('Password:')
hlr_tel.write(self.password+"\r")
#execute command
hlr_tel.read_until('maint at atcaHLRds0 /public/users/maint>')
hlr_tel.write(command+'\r')
#end session
data = hlr_tel.read_until('maint at atcaHLRds0
/public/users/maint>')
hlr_tel.write('exit'+'\r')
data2 = self.fix_return(data)
#return data
return data2
except Exception,error:
logs("error",str(err),'null')
def logs(self,log_type,log_data,ip_address):
"""
Log for errors
"""
try:
conn =
psycopg2.connect(database="hlr_proxy",user="postgres",host="localhost",
password="bb_server_1",port="5432") #connect to database
create_date = str(datetime.datetime.now())
cur = conn.cursor() # create cursor
cur.execute("insert into
hlr_logs(create_date,log_type,log_data,ip_address)values(%s,%s,%s,%s)",
(create_date,log_type,log_data,ip_address))
conn.commit()
cur.close()
conn.close()
finally:
try:
log_file = open(r"text_log.txt","a")
log_file.write(create_date+","+log_type+","+log_data+","+str(ip_address)+'\r
'+'\n')
log_file.close()
except Exception,error:
pass
-----Original Message-----
From: tutor-bounces+evosweet=hotmail.com at python.org
[mailto:tutor-bounces+evosweet=hotmail.com at python.org] On Behalf Of Steven
D'Aprano
Sent: 22 October 2011 15:55
To: tutor at python.org
Subject: Re: [Tutor] python telnet
Rayon wrote:
> Can I connect to a telnet session and return data without
> disconnecting the data session.
Isn't this the same question you asked back in June?
We tried to answer your question then, did you see our responses, and were
they useful?
At the time, you were complaining that the telnet session was too slow, but
you didn't tell us what you were actually doing. David Heiserca took a guess
as to what you were doing, and suggested what you should do again. Did you
see his response, and was it helpful?
You should show us the code you are using.
--
Steven
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list