Answer the question
In order to leave comments, you need to log in
Telnet client in python: how to implement command completion in the console by tab?
Hello everyone,
Actually, at the moment I have an example:
# telnet program example
import socket, select, string, sys
#main function
if __name__ == "__main__":
if(len(sys.argv) < 3) :
print 'Usage : python telnet.py hostname port'
sys.exit()
host = sys.argv[1]
port = int(sys.argv[2])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
# connect to remote host
try :
s.connect((host, port))
except :
print 'Unable to connect'
sys.exit()
print 'Connected to remote host'
while 1:
socket_list = [sys.stdin, s]
# Get the list sockets which are readable
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
for sock in read_sockets:
#incoming message from remote server
if sock == s:
data = sock.recv(4096)
if not data :
print 'Connection closed'
sys.exit()
else :
#print data
sys.stdout.write(data)
#user entered a message
else :
msg = sys.stdin.readline()
s.send(msg)
Answer the question
In order to leave comments, you need to log in
If I understand correctly what you need
import readline
texts = ['hello', 'world', 'readline']
def completer(text, state):
options = [x for x in texts if x.startswith(text)]
try:
return options[state]
except IndexError:
return None
readline.set_completer(completer)
readline.parse_and_bind("tab: complete")
while 1:
a = raw_input("> ")
print "You entered", a
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question